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

问题描述:

我已经创建了四种自定义帖子类型 - 视频,音频,程序,博客 - 具有各种自定义字段,并且我设法创建了一个自定义存档页面,根据帖子类型显示特定的自定义字段。当仅查看某个自定义帖子类型的存档时,存档页面运行良好。我遇到的问题是归档页面的类别。Wordpress:如何在类别归档页面中包含使用自定义帖子类型UI(CPT UI)制作的自定义帖子类型?

每个自定义帖子类型都可以访问全球'帖子'类别,因此我的所有类别都显示在每个帖子类型中。我希望能够查询某个类别并显示相关的帖子,而不管帖子类型如何。即“商业”类别可能拉起两个视频,一个音频帖子和一个博客。问题是我的分类页面现在是空的。

这里是 'archive.php' 我的电流回路:

​​

这抓住归档内容 '内容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>'; 

} 

我知道我缺少一个条件检查美食血腥,但我一直无法找到我的研究解决方案。任何帮助将不胜感激。 :)

+0

为什么不为每个帖子类型创建自定义分类标准? – ucheng

+0

@ucheng感谢您的回复!我创建不同分类法的问题是冗余。在这种情况下,将会有四个“商业”类别 - 每个类别都有一个。有没有办法拉出与一个类别相关的所有自定义帖子类型? –

+0

我添加了一个答案,不知道我是否理解正确。如果没有,请让我知道,我会更新答案。 – ucheng

因此,经过一番研究,我找到了解决方案。我需要将我的自定义帖子类型添加到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); 

} 

?> 

这和我的其他“内容archive.php”模板之间的区别是有条件的每个帖子类型。而不是检查is_post_type_archive('my custom post type')我正在检查get_post_type() == 'my custom post type'当前类别中每个帖子的帖子类型。

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

不知道我理解你的问题是否正确。如果要查询与一个类别相关的所有自定义帖子,可以在存档页面中使用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(); 
+0

感谢您的解决方案,但不幸的是它没有奏效。但是,我能够找到解决方案并发布。 –