首页 > 解决方案 > 使用帖子类别查询类别模板上的帖子

问题描述

我有一个名为“案例研究”的自定义帖子类型,用于案例研究和博客帖子的默认帖子类型(“帖子”)。我有一个名为“product_categories”的自定义分类法,用于博客文章和案例研究文章。

在我的类别模板“taxonomy-product_categories.php”上,我想将“博客”文章摘录与“案例研究”文章摘录分开,但我找不到查询文章存档的方法?

我已经尝试过 is_post_type_archive()、is_archive()、is_category() 和 is_tax(),但它们都没有过滤帖子。

我也尝试过查询帖子,但它只是加载了该帖子类型的所有帖子。

标签: wordpresscategoriesposts

解决方案


如果您使用这样的标准,您应该能够使用 while 循环中的 post 对象来区分帖子类型:

    <?php while ($query->have_posts()) {
      $query->the_post(); ?>

你可以在你的while循环中这样做:

    <div class="left-column">
       <?php
          if ($post->post_type === 'case-studies') {

            // Show your case study excerpts

          } else {

               // No content found message
          }

       ?>
     </div>

     <div class="right-column">
       <?php
          if ($post->post_type === 'post') {

            // Show your post excerpts

          } else {

               // No content found message
          }

       ?>
     </div>

推荐阅读