douguangxiang0363 2015-02-27 04:33
浏览 26

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

I have a custom post type in my WordPress site for my portfolio projects. The structure is to have a portfolio index listing portfolio categories and then child pages for the individual projects.

On the portfolio index, I am trying to show each portfolio category (there's 4 in total) with its title and the number of projects (child pages) within it. e.g. Photography – 16 projects.

The part I am stuck on is counting the number of child pages for each parent category when cycling through the loop. I have the standard loop functioning correctly as this:

// 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();
}

I have tried what seems like an endless amount of possibilities and feel as though I am close. The closest I can get is to display the same number on every category which appears to count all child pages and all categories, rather than just the count for the particular category being queried.

I have also tried all sorts of variations using the_post() and theID() or within each other and numerous others. Even tried swapping out get_pages() for get_post() and all variations I can think of without going round in circles, but I'm sure I must have missed the correct combination somewhere.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么