WordPress自定义帖子类型 - 列出子页面的数量

时间:2015-02-27 04:33:04

标签: php wordpress loops count children

我的WordPress网站上有一个自定义帖子类型用于我的投资组合项目。结构是有一个投资组合索引,列出投资组合类别,然后列出各个项目的子页面。

在投资组合指数上,我试图显示每个投资组合类别(共有4个),其中包含标题和项目数(子页面)。例如摄影 - 16个项目。

我坚持的部分是在循环循环时计算每个父类别的子页面数。我的标准循环正常运行如下:

// The Query
$the_query = new WP_Query(array(
    // Query only the custom post type ja_portfolio
    'post_type' => 'ja_portfolio',
    // Display only top-level parent pages (e.g. project categories)
    'post_parent' => 0, 
    'orderby' => 'menu_order',
    'order' => 'ASC'
));
// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        // Other parts of the loop functioning fine...

        // Get number of child pages *struggling here*
        $children = get_pages('child_of='.$the_query->the_post());
        if( count( $children ) != 0 ){
            echo count($children);
        };

        // Rest of code functioning fine here...
} else {
    // no posts found
    // Restore original Post Data
    wp_reset_postdata();
}

我已经尝试了似乎无穷无尽的可能性,感觉好像我很亲近。我能得到的最接近的是在每个类别上显示相同的数字,这些数字似乎计算所有子页面和所有类别,而不仅仅是要查询的特定类别的计数。

我还尝试了使用the_post()theID()或彼此之间以及众多其他变体的各种变体。甚至尝试将get_pages()替换为get_post()以及我可以想到的所有变化,而不是绕圈,但我相信我一定错过了某个地方的正确组合。

0 个答案:

没有答案