您的博客上有可靠的类别结构吗?如果是这样,您可能根本不需要“相关帖子”部分 – 您只需显示同一类别的最新帖子即可。
在这篇文章中,我们将介绍“此类别的更多内容”部分,这是“相关帖子”(我们之前介绍过)的替代选项。
表明你还有更多话要说
如果您将帖子按类别组织得很好,您可能会发现拥有帖子类别中的帖子列表很有用。
“相关帖子”并不总是答案:如果您的网站上的帖子按类别分隔,那么“相关帖子”部分可能会“打破”这种分隔。
例如,如果您有一个关于不同职业群体的博客,则无法在有关信息学的帖子下将有关纺织行业的新闻显示为“相关新闻”。同一类别的许多最新帖子会更相关,对吧?
创建“此类别的更多内容”列表
正如您可能已经猜到的,列出帖子类别中的最新帖子比根据帖子标签显示相关帖子要容易得多。我们只需要获取帖子的类别并列出该类别中的许多帖子,不包括访问者刚刚阅读的帖子。我们可以在 get_posts() 函数中传递的参数包含我们需要的一切。
<?php
// \”More from This Category\” list by Barış Ünver @ Wptuts+
function wptuts_more_from_cat( $title = \”More From This Category:\” ) {
global $post;
// We should get the first category of the post
$categories = get_the_category( $post->ID );
$first_cat = $categories[0]->cat_ID;
// Let\’s start the $output by displaying the title and opening the <ul>
$output = \'<div id=\”more-from-cat\”><h3>\’ . $title . \'</h3>\’;
// The arguments of the post list!
$args = array(
// It should be in the first category of our post:
\’category__in\’ => array( $first_cat ),
// Our post should NOT be in the list:
\’post__not_in\’ => array( $post->ID ),
// …And it should fetch 5 posts – you can change this number if you like:
\’posts_per_page\’ => 5
);
// The get_posts() function
$posts = get_posts( $args );
if( $posts ) {
$output .= \'<ul>\’;
// Let\’s start the loop!
foreach( $posts as $post ) {
setup_postdata( $post );
$post_title = get_the_title();
$permalink = get_permalink();
$output .= \'<li><a href=\”\’ . $permalink . \’\” title=\”\’ . esc_attr( $post_title ) . \’\”>\’ . $post_title . \'</a></li>\’;
}
$output .= \'</ul>\’;
} else {
// If there are no posts, we should return something, too!
$output .= \'<p>Sorry, this category has just one post and you just read it!</p>\’;
}
// Let\’s close the <div> and return the $output:
$output .= \'</div>\’;
return $output;
}
?>
完成!您可以将此函数包含在您的 functions.php 文件中(或将其保存为单独的插件)并回显它(如 ) 在 single.php 文件中的任何位置。
结论
是的,内容可能是“王”,但孤独的王是软弱的王,人们可能不会尊重那个“王”。
您认为还有更多的页面元素可以帮助“王”吗?请在下面发表您的评论 – 与我们分享您的想法对您来说始终很重要!
以上就是快速提示:内容之后 – 同一类别的更多内容的详细内容,更多请关注悠久资源其它相关文章!