douyou1857 2016-04-13 13:59
浏览 50

get_categories中的Wordpress WP_Query无法使用CPT

EDIT: After changing the tax_query to slug it now outputs posts in the first loop (the first cat) but does not in the next tab/cat which I know has many published posts assigned to it.

I created a shortcode that is meant to grab all the categories in a CPT and out them into bootstrap tabs with a certain amount of posts per tab.

To do this I do get_categories for the tabs nav (Works fine). Then I do get_categories again to output the tabs content (Also works fine) with a wp_query inside each where I grab the posts. This is what is not working. No output.

If I echo the category name or any data I can grab from the get_categories it echo's it. So it seems the issue is with the WP_Query.

Here is the code:

add_shortcode( 'tabbed_grid_articles', 'tabbed_grid_articles' );
function tabbed_grid_articles($args = array()){
    global $post;
    $current_id = $post->ID;
    $defaults = apply_filters( 'dy_tabbed_grid_default_args', array(
        'posts_per_page'   => 1,
        'orderby'       => 'date',
        'order'         => 'DESC',
        'echo'          => true,
        'post_type'     => 'playbooks',
        'category'      => 0,
        'hide_empty'    => true,
        'exclude'       => '',
        'include'       => '',
        'taxonomy'      => 'playbooks_category',

    ));

    $args = wp_parse_args( $args, $defaults );

    $cat_args = array(
        'taxonomy'      => $args['taxonomy'],
        'orderby'       => 'menu_order',
        'order'         => 'ASC',
        'hierarchical'  => true,
        'parent'        => $args['category'],
        'hide_empty'    => $args['hide_empty'],
        'child_of'      => $args['category'],
        'exclude'       => $args['exclude'],
        'include'       => $args['include']
    );

    $categories = get_categories($cat_args);
    ?> <ul class="nav nav-tabs" role="tablist"> <?php
        $count = 0;
        foreach($categories as $category){
            $targetID = str_replace(' ', '-', $category->cat_name);
            ?>        <!-- Nav tabs -->
            <li role="presentation" class="<?php if($count == 0){echo 'active'; $count ++;} ?>"><a href="#tab-<?php echo $targetID ?>" aria-controls="tab-<?php echo $targetID ?>" role="tab" data-toggle="tab"><?php echo $category->cat_name ?></a></li>
            <?php
        }
       // wp_reset_postdata();
        ?>     </ul>
    <!-- Tab panes -->
    <div class="tab-content">
        <?php
        $count = 0;
        foreach($categories as $category) {
            $targetID = str_replace(' ', '-', $category->cat_name);
            $args['tax_query'] = array(array(
                    'taxonomy' => $args['taxonomy'],
                    'field' => 'slug',
                    'terms' => $category->cat_name,
                ),
            );
//            $args['cat'] = $category->cat_ID;
            $grid_query = new WP_Query($args);
            ?>
            <div role="tabpanel" class="tab-pane <?php if($count == 0){echo 'active'; $count ++;} ?>" id="tab-<?php echo $targetID ?>">
                <?php
                if($grid_query->have_posts()) {
                    while ($grid_query->have_posts()) {
                        $grid_query->the_post();
                        echo get_the_title();
                        //echo '1<br>';
                        //echo $args['cat'];
                        //echo $category->cat_name;
                        //echo get_the_excerpt();
                        echo '<br>';
                        //the_excerpt();
                    }
                    wp_reset_postdata();
                }
                ?></div>
            <?php
        }
        ?>



    </div>

    <?php
}

I tried $args['cat'] = $category->cat_ID; instead of tax_query but neither work. If I comment them both out then I get all the posts from the cpt. Currently just echoing the excerpt...

Any ideas?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-04-13 14:32
    关注

    Solved. I took for granted that the slug was the same as the name and it was not. I think I only changed the slug to be the name by mistake when debugging at first when it was not working. Anyway, changed 'terms' => $category->cat_name, to 'terms' => $category->slug, and it worked.

    Hope this helps someone in the future.

    评论

报告相同问题?

悬赏问题

  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面