【问题标题】:Wordpress: How do I include custom post types made with Custom Post Type UI (CPT UI) in category archive pages?Wordpress:如何在类别存档页面中包含使用自定义帖子类型 UI (CPT UI) 制作的自定义帖子类型?
【发布时间】:2017-04-04 02:42:29
【问题描述】:

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

每种自定义帖子类型都可以访问全局“帖子”类别,因此我的所有类别都会显示在每种帖子类型中。无论帖子类型如何,我都希望能够查询一个类别并显示相关的帖子。即“商业”类别可能会拉出两个视频、一个音频帖子和一个博客。问题是我的类别页面现在是空的。

这是我当前在“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”构建帖子内容的函数:

// 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>';

}

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

【问题讨论】:

  • 为什么不为每个帖子类型创建自定义分类法?
  • @ucheng 感谢您的回复!我创建不同分类法的问题是冗余。在这种情况下,将有四个“业务”类别——每个帖子类型一个。有没有办法提取与一个类别相关的所有自定义帖子类型?
  • 我添加了一个答案,不确定我是否理解正确。如果没有,请告诉我,我会更新答案。

标签: php wordpress custom-post-type archive categories


【解决方案1】:

不确定我是否正确理解了您的问题。如果要查询某一类的所有自定义帖子,可以在归档页面使用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();

【讨论】:

    【解决方案2】:

    所以,经过一番研究,我找到了解决方案。我需要将我的自定义帖子类型添加到 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”模板之间的区别在于每种帖子类型的条件。我现在不是检查is_post_type_archive('my custom post type'),而是使用get_post_type() == 'my custom post type' 检查当前类别中每个帖子的帖子类型。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2013-06-02
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多