您好,欢迎光临! 推荐您使用Chrome浏览器访问本站。

WordPress通过分类显示相关文章列表

将下面的代码放到single.php文件里面的<?php comments_template( ”, true ); ?>上面,位置和样式请根据自己想法进行调整。

&lt;div id=&quot;recent-post&quot;&gt;
&lt;h2&gt;相关文章列表&lt;/h2&gt;
&lt;ul&gt;
&lt;?php
    $catid = get_the_category(); //通过这个函数可以获得该文章下的分类信息

    $newcat_id = $catid[0]-&gt;cat_ID; //获取得到该文章下的分类ID

    $recent = new WP_Query(&quot;showposts=5&amp;orderby=ID&amp;cat=&quot;.$newcat_id);

    while($recent-&gt;have_posts()) : $recent-&gt;the_post();
?&gt;
&lt;li&gt;&lt;a title=&quot;&lt;?php the_title(); ?&gt;&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;

&lt;?php   endwhile; wp_reset_query();  ?&gt;
&lt;/ul&gt;
&lt;/div&gt;
//showposts=5,5表示显示5条相关文章,请根据自己需要进行设置。

效果如本文章尾部。

您可能也喜欢