快速提示:内容之后-同一类别的更多内容

   2025-07-19 admin00100

您的博客上有可靠的类别结构吗?如果是这样,您可能根本不需要“相关帖子”部分-您只需显示同一类别的最新帖子即可。

在这篇文章中,我们将介绍“此类别的更多内容”部分,这是“相关帖子”(我们之前介绍过)的替代选项。

表明你还有更多话要说

如果您将帖子按类别组织得很好,您可能会发现拥有帖子类别中的帖子列表很有用。

“相关帖子”并不总是答案:如果您的网站上的帖子按类别分隔,那么“相关帖子”部分可能会“打破”这种分隔。

例如,如果您有一个关于不同职业群体的博客,则无法在有关信息学的帖子下将有关纺织行业的新闻显示为“相关新闻”。同一类别的许多最新帖子会更相关,对吧?

创建“此类别的更多内容”列表

正如您可能已经猜到的,列出帖子类别中的最新帖子比根据帖子标签显示相关帖子要容易得多。我们只需要获取帖子的类别并列出该类别中的许多帖子,不包括访问者刚刚阅读的帖子。我们可以在get_posts()函数中传递的参数包含我们需要的一切。

<?php//"MorefromThisCategory"listbyBarışÜnver@Wptuts+functionwptuts_more_from_cat($title="MoreFromThisCategory:"){global$post;//Weshouldgetthefirstcategoryofthepost$categories=get_the_category($post->ID);$first_cat=$categories[0]->cat_ID;//Let'sstartthe$outputbydisplayingthetitleandopeningthe<ul>$output='<divid="more-from-cat"><h3>'.$title.'</h3>';//Theargumentsofthepostlist!$args=array(//Itshouldbeinthefirstcategoryofourpost:'category__in'=>array($first_cat),//OurpostshouldNOTbeinthelist:'post__not_in'=>array($post->ID),//...Anditshouldfetch5posts-youcanchangethisnumberifyoulike:'posts_per_page'=>5);//Theget_posts()function$posts=get_posts($args);if($posts){$output.='<ul>';//Let'sstarttheloop!foreach($postsas$post){setup_postdata($post);$post_title=get_the_title();$permalink=get_permalink();$output.='<li><ahref="'.$permalink.'"title="'.esc_attr($post_title).'">'.$post_title.'</a></li>';}$output.='</ul>';}else{//Iftherearenoposts,weshouldreturnsomething,too!$output.='<p>Sorry,thiscategoryhasjustonepostandyoujustreadit!</p>';}//Let'sclosethe<div>andreturnthe$output:$output.='</div>';return$output;}?>
 
举报收藏 0打赏 0评论 0
 
更多>同类资讯
推荐图文
推荐资讯
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  版权声明  |  网站地图  |  RSS订阅
Powered By DESTOON