简体   繁体   English

wordpress-帖子页面上相同类别的其他帖子

[英]wordpress - other posts of the same category on post page

i need to link the lasts 5 posts of the same category after the post content in wordpress. 我需要在wordpress中的帖子内容之后链接相同类别的最后5个帖子。 Anyone can help me? 有人可以帮助我吗? thanks 谢谢

You can obtain anithing you want using WP_Query . 您可以使用WP_Query获得所需的内容 It is very flexible and you should definitely start using it. 它非常灵活,您绝对应该开始使用它。 It is just like the SQL queryes. 就像SQL查询一样。

Before the loop declare some var that will hold post category. 在循环之前,声明一些将保存帖子类别的变量。 Than query posts with that cat. 比查询与那只猫的帖子。

Here is example: 这是示例:

<?php
$cat_id = 0; // declare var
if(have_posts()):
  while(have_posts()):
    // do what you usually do
    $cat_id = $post->post_category;
  endwhile;
endif;

// here you will get posts and make html output
$last_in_cat = get_posts('posts_per_page=5&cat='.$cat_id);
foreach($last_in_cat as $cat_post):
?>
<a href="<?php $cat_post->guid ?>"><?php $cat_post->post_title ?></a>
<?php endforeach; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM