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 wordp开发者_StackOverflowress. Anyone can help me? thanks
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; ?>
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.
精彩评论