首页 文章

仅显示自定义类别的帖子

提问于
浏览
1

我有一个名为照片库的自定义帖子类型 . 在该帖子类型中,我注册了一个名为video的分类 . 然后我在“个人”和“商业”视频下创建了两个类别 . 现在我想只显示页面部分的商业类别帖子 . 我怎样才能做到这一点?这是我尝试但无法正常工作的代码

<?php 

                            $args = array(
                        'post_type'=>'photo_gallerys',
                        'post_status'=>'publish',
                        'video'=>'commercial',
                        'posts_per_page'=>-1,
                        'paged'=>get_query_var('paged')
                         );
// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

       <?php the_title(); ?>


    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

2 回答

  • 0

    试试这段代码,希望你能找到解决方案

    <?php
        $tax_post_args = array(
                'post_type' => 'your post type name',
                'posts_per_page' => 999,
                'order' => 'ASC',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'your taxonomy name',
                        'field' => 'id',
                        'terms' => your category id
                    )
                )
            );
    
    $tax_post_qry = new WP_Query($tax_post_args);
    
        while($tax_post_qry->have_posts()) :
               $tax_post_qry->the_post();
               the_title();
        endwhile;
        ?>
    

    如果你想从类别slug获得帖子,那么在tax_query数组中使用这个代码

    'field' => 'slug',
    'terms' => 'your category slug'
    
  • 0

    在wp查询参数中使用 'cat' => "your_cat_id .

    例如,如果我想显示仅商业类别的帖子和商业广告 category id 21 ,那么我将编写查询以显示帖子,如下所示:

    <?php 
     $args = array(
    'post_type'=>'photo_gallerys',
    'post_status'=>'publish',
    'video'=>'commercial',
    'cat' => 21,
    'posts_per_page'=>-1,
    'paged'=>get_query_var('paged')
     );
    

相关问题