按最新的子级对帖子进行排序,同时保持层次结构不变

时间:2011-09-08 作者:user648519

我在wordpress帖子中有以下层次结构(例如):

-> Post 1 (pubdate: 2011-01-01)
 --> Post 5 (pubdate: 2011-01-02)
  ---> Post 6 (pubdate: 2011-01-03)
   ----> Post 7 (pubdate: 2011-01-04)

-> Post 2 (pubdate: 2011-03-01)
 --> Post 8 (pubdate: 2011-03-02)

-> Post 3 (pubdate: 2011-02-01)
 --> Post 9 (pubdate: 2011-02-02)
  ---> Post 10 (pubdate: 2011-02-03)

-> Post 4 (pubdate: 2011-04-01)
我现在想要实现的是按照最新的子帖子的发布日期对父帖子进行排序。重要的是层次结构应该保持完整。这将导致示例中的以下顺序(从最新到最旧):

-> Post 4 (pubdate: 2011-04-01)

-> Post 2 (pubdate: 2011-03-01)
 --> Post 8 (pubdate: 2011-03-02)

-> Post 3 (pubdate: 2011-02-01)
 --> Post 9 (pubdate: 2011-02-02)
  ---> Post 10 (pubdate: 2011-02-03)

-> Post 1 (pubdate: 2011-01-01)
 --> Post 5 (pubdate: 2011-01-02)
  ---> Post 6 (pubdate: 2011-01-03)
   ----> Post 7 (pubdate: 2011-01-04)
你知道我该怎么做吗?谢谢

EDIT: 我根据Rarst的问题添加了以下部分,即如何检索结构。

我通过以下查询检索帖子:

$args = array(
  \'cat\' => 7,
  \'post_type\' => \'articles\', 
  \'orderby\' => \'post_date\', 
  \'posts_per_page\' => -1, 
  \'order\' => \'DESC\' 
); 
$records = get_posts($args);
foreach($records as $post) {
  get_template_part( \'index\', \'article\' );
}
然后我使用此函数检查一篇文章有多少个家长。。。

// count amount of parents of a post
function usr_count_children($cid) {
  $childrenArray = count(get_pages("post_type=articles&child_of=".$cid));
  return $childrenArray;
}
。。。并将返回的数字作为css类添加到文章的html元素中(然后根据该元素设置样式)。

EDIT 2:

好吧,这就是我到目前为止得到的:

    $parentPosts = get_posts(array(
    \'cat\' => 7, 
    \'post_type\' => \'articles\',
    \'post_status\' => \'publish\',
    \'post_parent\' => 0, 
    \'posts_per_page\' => -1,
    \'orderby\' => \'post_date\',
    \'order\' => \'DESC\' ));

foreach ($parentPosts as $post){

    echo "+ ".$post->post_title." ID: ".$post->ID."<br/>";

    // Look up newest child (query, sort by date, limit to one)
    $newestChild = get_pages(array(
        \'child_of\' => $post->ID,
        \'post_type\' => \'articles\',
        \'post_status\' => \'publish\',
        \'sort_column\' => \'post_date\',
        \'sort_order\' => \'DESC\',
        \'number\' => 1 ));

    foreach($newestChild as $child){
        echo "– ".$child->post_title." ID: ".$child->ID."<br/>";
    }
}
我一直在使用第二个查询(获取最新的子项),除非我删除它,否则它不会返回任何子项\'number\' => 1. 我确实尝试了一些事情(比如使用WP\\u查询,我甚至尝试了原始的SQL查询),但我还是无法理解。任何人

1 个回复
SO网友:Rarst

首先,如何检索和回显此结构?

我认为您不能通过这样的方式进行查询(如果没有一些糟糕的SQL),因此可能更容易:

检索父帖子

结束

相关推荐

POSTS_NAV_LINK()不显示任何内容

我很难让posts\\u nav\\u link()显示任何内容。下面是我的索引。php模板文件,我正在使用permalinks(index.php/2011/08/26/sample-post/ 格式),并且我当前已获得Blog pages show at most 设置为-1.有人知道为什么链接不起作用吗?index.php <?php get_header(); $args = array(\'posts_per_page\' => 4,\'paged\'