2013-03-19 63 views
4

我有几个配置文件,允许用户发布新闻,我想要显示在一个长的饲料中,按日期顺序排序。按几个配置文件中的日期排序新闻

目前,此循环遍历每个配置文件,列出来自该配置文件的新闻,然后移动到下一个配置文件,列出他们的新闻等。它并没有把它混合起来。

如何将每个配置文件中的所有新闻混合在一起并按日期排序?

<?php global $post; 
$args = array('numberposts' => -1); 
$myposts = get_posts($args); 
foreach($myposts as $post) : setup_postdata($post); ?> 
    <?php while(the_repeater_field('news')): ?> 
     <div class="social-news-item"> 
     <p class="social-news"><?php the_sub_field('news_description'); ?></p> 
    <p class="social-company"><?php echo the_title(); ?></p> 
     </div> 
    <?php endwhile; ?> 
<? endforeach; wp_reset_postdata(); ?> 

日期字段是:

<?php the_sub_field('date'); ?>

+0

我不知道WordPress的,也许这是一个愚蠢的问题,但有可能先拿到的所有帖子到一个数组,然后对它们进行排序? – Voitcus 2013-03-21 12:08:12

+0

@Voitcus问题中的循环获取所有帖子,只是不知道如何放入数组并按日期排序? – Rob 2013-03-21 12:49:19

+0

我会在答案中输入它,因为我在这里没有空间,但不认为它是答案。 – Voitcus 2013-03-21 12:52:12

回答

2

正如我在评论中写道,我不知道WordPress的,也许什么是错的,但这个想法可能是好的。

如果可能的话,你可以先把所有的帖子都收集起来,然后对它们进行排序。

要将它们分组,请使用临时数组来获取对象。所以不要输出你的div s,而是将它们“缓冲”在数组中。

例如,如果数组名称将是$posts你可以在你foreach循环do: (我希望能够在the_sub_field('news_description');你错过echo行,在这种情况下它不会工作)

$var = &$posts[]; // this creates a new item in array, and $var is this element 
$var->description = the_sub_field('news_description'); // ***** 
$var->title = the_title(); 
$var->date = ... // it is important, I don't know if you have it 

因此,最后你的阵列将拥有所有用户(或配置文件)的所有帖子。现在使用usort function对它进行排序。您将需要编写自己的排序功能,在手册中是“CMP”,所以我用同一个名字:

​​

,并调用它

usort($posts, 'cmp'); 

然后,你需要输出数组中另一个foreach循环。

但正如我所说,我不知道wordpress是否允许这样做。

3

如果你想以编程方式更改顺序,看看各种array sorting functions in PHP,尤其是

  • uasort() - 排序与用户自定义的比较函数对数组中,并保持索引关系
  • uksort() - 排序按键排列使用用户定义的比较函数
  • usort() - 使用用户定义的比较函数按值排序数组

但WordPress的结帐中给出的链接这一http://codex.wordpress.org/Template_Tags/get_posts

例子:

默认:

<?php 
    $args = array(
    'posts_per_page' => 5, 
    'numberposts'  => 5, 
    'offset'   => 0, 
    'category'  => '', 
    'orderby'   => 'post_date', 
    'order'   => 'DESC', 
    'include'   => '', 
    'exclude'   => '', 
    'meta_key'  => '', 
    'meta_value'  => '', 
    'post_type'  => 'post', 
    'post_mime_type' => '', 
    'post_parent'  => '', 
    'post_status'  => 'publish', 
    'suppress_filters' => true); 
?> 

用途:

<?php $posts_array = get_posts($args); ?> 

这可以帮助你。

+0

这个消息不是帖子(所以不能在帖子日期后去),帖子是一个拥有一个名为news的自定义字段的配置文件。 – Rob 2013-03-28 10:58:51

1

我假设你的意思是'作者',当你提到'简介'。

一旦你在$ myposts所有帖子

$myposts = get_posts($args); 

您完成所有这些迭代与

foreach($myposts as $key => $post) 

(注意我已经添加了$键)

现在你可以创建你自己排列的日期作为键

$my_array[$myposts[$key]->post_date][] = ...whatevever you want in the end 

随意添加您想要显示的任何数据。我们只是把作者和职位的标题为例:

$the_item[$myposts[$key]->post_author] = $myposts[$key]->post_title; 
$my_array[$myposts[$key]->post_date][] = $the_item; 

这将导致一个数组,看起来像这样:

[2007-11-19 22:46:37] => Array 
    (
     [0] => Array 
      (
       [3] => Title 
       [2] => Another title 
      ) 

    ) 

[2007-11-11 11:11:11] => Array 
    (
     [0] => Array 
      (
       [3] => Yet another title 
       [2] => Foo 
      ) 

     [1] => Array 
      (
       [3] => Bar 
       [2] => Yuck 
      ) 

所有职位目前由日期排序由不同作者的帖子'混合'。随意使用任何排序功能(如上所述)。您可能希望通过使用shuffle()来添加一点随机性... 为了显示帖子,您将采用相反的方式:遍历创建的数组并打印您包含的数据(例如作者,标题,内容等)。 作为参考,这是后物体的样子:

myposts:Array 
(
    [0] => stdClass Object 
     (
      [ID] => 1455 
      [post_author] => 3 
      [post_date] => 2013-03-27 22:16:33 
      [post_date_gmt] => 2013-03-27 22:16:33 
      [post_content] => Content 
      [post_title] => Title 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => closed 
      [ping_status] => closed 
      [post_password] => 
      [post_name] => title 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2013-03-27 22:16:42 
      [post_modified_gmt] => 2013-03-27 22:16:42 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://www.abc.com/wordpress/?p=1455 
      [menu_order] => 0 
      [post_type] => post 
      [post_mime_type] => 
      [comment_count] => 0 
      [member_access_visibility] => default 
      [filter] => raw 
     ) 

希望这可以帮助您解决您的问题!

干杯

JD

+0

更好的方法是使用时间戳作为具有Strtotime()函数的数组键。接下来是一个简单的ksort($ array),根据键(=日期)对它们进行排序。 – 2013-03-28 00:16:28

+0

@JohnDoeherty该消息是帖子的自定义字段。这篇文章是一个公司的完整档案,所以到日期为止是毫无意义的。 – Rob 2013-03-28 11:00:08

+0

我不明白:在你的问题中,你要求按日期排序,那么怎么去做日期毫无意义?您的自定义字段将与帖子关联,以便发布日期仍然与您的自定义字段相关。如果您在自定义字段中存储其他日期,上述方法仍然适用。 – 2013-03-28 14:05:17

0

你只需要一个get_posts()函数调用。

创建一个你想用字符串分隔的所有用户标识符的字符串。现在

您可以添加到您的ARGS您的订单一起:

$args = array(
    'numberposts' => -1, 
    'author' => '1,2,3,4,5', 
    'orderby' => 'date' // date is the default anyway, so shouldn't matter 
); 

然后你get_posts()的通话将包括由这些作者的所有帖子。

HTH

+0

我无法按日期排序,因为该帖子是完整的公司配置文件,新闻是该帖子内的自定义字段。因此,它需要将所有新闻消除,然后按与该新闻行相关的日期进行排序。我正在使用www.advancedcustomfields.com和新闻中继器领域。 – Rob 2013-03-28 11:01:33