在你的单曲上。php模板,您需要加载类别术语,然后运行查询以获取其他帖子。在我的示例中,我从单个类别加载帖子。我们安装了Wordpress SEO插件,如果您决定选择几个类别,可以选择一个主要类别。
$related_articles = array();
$categories = get_the_category();
if($categories) {
$category_id = $categories[0]->term_id;
if(count($categories) > 1 && class_exists(\'WPSEO_Primary_Term\')) {
$primary_term = new WPSEO_Primary_Term(\'category\', get_the_id());
$category_id = $primary_term->get_primary_term();
}
$related_articles = get_posts(array(
\'category\' => $category_id,
\'exclude\' => $post->ID,
\'numberposts\' => 3,
\'order\' => \'DESC\',
\'orderby\' => \'date\',
\'post_status\' => \'published\',
\'post_type\' => \'post\'
));
}
然后,您可以循环浏览相关帖子(如果有):
<?php if($related_articles): ?>
<section class="related-articles">
<?php foreach($related_articles as $post): ?>
<?php setup_postdata($post); ?>
<article>
<h2><?php the_title(); ?></h2>
</article>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</section>
<?php endif; ?>
希望有帮助