【问题标题】:Show only post of custom category仅显示自定义类别的帖子
【发布时间】:2014-09-09 02:03:45
【问题描述】:

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

                      <?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; ?>

【问题讨论】:

    标签: wordpress categories taxonomy


    【解决方案1】:

    试试这段代码希望你能找到解决办法

        <?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'
    

    【讨论】:

      【解决方案2】:

      在您的 wp 查询参数中使用 'cat' =&gt; "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')
       );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        相关资源
        最近更新 更多