如何显示特定自定义帖子类型类别的帖子

时间:2017-02-22 01:07:26

标签: wordpress

我试图显示我在自定义帖子类型中制作的类别的帖子。

这是我当前的循环代码,

我想在公告类型

中显示奖项类别的帖子
<?php $loop = new WP_Query( array( 'posts_per_page' => 99,'post_type' => 'annoucements','orderby' => 'date','order' => 'ASC','ignore_sticky_posts' => 1, 'paged' => $paged ) ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title()?>                                                         
<?php endwhile; endif; wp_reset_postdata();?>   

2 个答案:

答案 0 :(得分:0)

如果你想从一个类别中获取帖子,那么你必须在project-info-reports:dependencies个参数中传递类别slug。

WP_Query

参考:WP_Query: Category Parameters

希望这有帮助!

答案 1 :(得分:0)

You can use this code for display posts from specific custom post type category(taxonomy).

According to extensive and long running trac ticket #12702, custom post types don't (and likely won't) support sticky functionality.

Please let me know if you found any issues in this code. Thanks.

<?php
    $options = array(
        'post_type' => 'annoucements',
        'posts_per_page' => 99,
        'orderby' => 'date',
        'order' => 'ASC',
        'paged' => $paged,
        'tax_query' => array(
            array(
                'taxonomy' => 'taxonomy_cat', // Here I have set dummy taxonomy name like "taxonomy_cat" but you must be set current taxonomy name of annoucements post type. 
                'field' => 'name',
                'terms' => 'awards'
            )
        )
    );
    $query = new WP_Query( $options );
    if ( $query->have_posts() ) :
        while($query->have_posts()) : $query->the_post();
            the_title();
        endwhile; wp_reset_query();
    endif;
    ?>