简体   繁体   中英

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. Anyone can help me? thanks

You can obtain anithing you want using WP_Query . It is very flexible and you should definitely start using it. It is just like the SQL queryes.

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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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