按自定义帖子类型和类别显示帖子列表

时间:2016-06-01 10:39:53

标签: php wordpress post

您好我需要按帖子类型和类别显示帖子列表,我有这样的代码,但它没有正常工作:

<?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename&cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?>

此代码显示来自&#34; posttypename&#34;的帖子,但它会显示该自定义帖子类型的所有帖子,但我只需显示来自&#34; categoryname&#34;

的帖子

整个代码如下所示:

 <?php $catquery = new WP_Query( 'posts_per_page=999&post_type=posttypename%cat=categoryname' ); while($catquery->have_posts()) : $catquery->the_post(); $i = 1; ?> <?php if($i == 1) : ?> <div class="">content of the post</div> <?php endif; ?> <?php $i++; endwhile; ?>

2 个答案:

答案 0 :(得分:0)

像这样使用WP_Query

$catquery = new WP_Query(array(
    'post_type'      => 'posttypename',
    'posts_per_page' => -1,
    'category_name'  => 'categoryname',
    // 'cat'         => cat ID here
));
if( $catquery->have_posts() ){
    while($catquery->have_posts()){
        $catquery->the_post();
        // your stuff
    }
    wp_reset_postdata();
}

答案 1 :(得分:0)

尝试以下代码

<?php 
$catquery = new WP_Query(array(
    'post_type'      => 'posttypename',
    'posts_per_page' => -1,
    'category_name'  => 'categoryname'    
));
if( $catquery->have_posts() ){
    $i = 1; 
    while($catquery->have_posts()){
        if($i == 1){ ?>
            <div class="">content of the post</div> 
        <?php } ?> 
        $i++; 
    }
    wp_reset_postdata();
}
?>