【发布时间】: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