Wordpress:如何在类别存档页面中包含使用自定义 Post 类型 UI (CPT UI) 制作的自定义 post 类型?

Wordpress: How do I include custom post types made with Custom Post Type UI (CPT UI) in category archive pages?

我已经创建了四种自定义 post 类型——视频、音频、程序、博客——具有各种自定义字段,并且我已经设法创建了一个自定义存档页面,该页面显示某些自定义字段,具体取决于post 类型。仅查看特定自定义 post 类型的存档时,存档页面运行良好。我遇到的问题是类别的存档页面。

每个自定义 post 类型都可以访问全局 'post' 类别,因此我的所有类别都显示在每个 post 类型中。我希望能够查询类别并显示关联的 posts 而不管 post 类型。即 'business' 类别可能会拉出两个视频、一个音频 post 和一个博客。问题是我的类别页面现在是空的。

这是我在 'archive.php' 中的当前循环:

<?php
if ( have_posts() ) : ?>


<div class="container">

    <div class="row">

        <?php
        /* Start the Loop */
        while ( have_posts() ) : the_post();

            get_template_part( 'template-parts/content-archive', get_post_format() );

        endwhile;

        the_posts_navigation(); ?>

    </div><!-- .row -->

</div><!-- .container -->

<?php endif; ?>

这会抓取存档内容的模板'content-archive.php':

<?php
/**
 * Get featured posts from Archives 
 */

if( is_post_type_archive( 'videos' ) ) {

    $video_image = get_field( 'video_still_image' ); 
    print_archive_post($video_image);

} elseif( is_post_type_archive( 'programs') ) {

    $program_featured_image = get_field('program_featured_image');
    print_archive_post($program_featured_image);

} elseif( is_post_type_archive( 'audio') ) {

    $audio_featured_image = get_field('audio_featured_image');
    print_archive_post($audio_featured_image);

} elseif( is_post_type_archive( 'blogs') ) {

    $blog_featured_image = get_field('blog_featured_image');
    print_archive_post($blog_featured_image);

}

?>

而且,这是我在 'functions.php' 中为 post 构建内容的函数:

// Function to print archive type posts
function print_archive_post($image) {

  echo '<div class="col-md-4 featured-thumb-container">';
    echo '<a href="' . get_the_permalink() . '"><span class="featured-thumb" style="background: url(' . $image . ')"></span></a>';
    echo '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>';

    // The category list
    $post_cats= array();
    $categories = get_the_category();

    echo '<p class="category-list">';

      foreach($categories as $cat) :
        echo '<a class="category-link" href="' . get_home_url() . '/category/' . $cat->slug . '">' . $cat->cat_name . '</a><span>,</span> ';
      endforeach;

    echo '</p>';

  echo '</div>';

}

我知道我缺少检查类别的条件,但我无法在我的研究中找到解决方案。任何帮助将不胜感激。 :)

不确定我是否正确理解了你的问题。如果要查询某个分类相关的所有自定义post,可以在存档页面使用WP_Query

  //if you're on a category archive, it will return the category object
  $category_object = get_queried_object();
  $args = array(
      'post_type' => 'your-cpt',
      'cat' => $category_object->term_id
  );

  $wp_query = new WP_Query( $args );

  if ( $wp_query->have_posts() ) {

    while ( $wp_query->have_posts() ) {   
        $wp_query->the_post();
        //echo your posts
    }

  } else {
      // aww, no posts... do other stuff
  }
  wp_reset_postdata();

所以,经过一番研究,我找到了解决方案。我需要将我的自定义 post 类型添加到 Wordpress 的内置类别和标签中。这是我添加到 functions.php 文件中的代码:

// Add custom post types to default WP categories and tags
function add_custom_types_to_tax( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

        // Get all your post types
        $post_types = get_post_types();

        $query->set( 'post_type', $post_types );
        return $query;
    }
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

我在这里找到了代码:https://premium.wpmudev.org/blog/add-custom-post-types-to-tags-and-categories-in-wordpress/

此外,我创建了一个 category.php 文件,仅用于分类档案。我使用了与我的 archive.php 文件相同的代码,但我换入了一个新的类别内容模板文件:'content-category-archive.php'。这是该模板的代码:

<?php
/**
 * Get featured posts from Category Archives 
 */

if( get_post_type() == 'videos' ) {

    $video_image = get_field( 'video_still_image' ); 
    print_archive_post($video_image);

} elseif( get_post_type() == 'programs' ) {

    $program_featured_image = get_field('program_featured_image');
    print_archive_post($program_featured_image);

} elseif( get_post_type() == 'audio' ) {

    $audio_featured_image = get_field('audio_featured_image');
    print_archive_post($audio_featured_image);

} elseif( get_post_type()== 'blogs' ) {

    $blog_featured_image = get_field('blog_featured_image');
    print_archive_post($blog_featured_image);

}

?>

此模板与我的其他 'content-archive.php' 模板的区别在于每个 post 类型的条件。我现在不再检查 is_post_type_archive('my custom post type'),而是使用 get_post_type() == 'my custom post type'.

检查当前类别中每个 post 的 post 类型

我希望这对可能遇到同样问题的其他人有所帮助。 :)