I am writing a code to display the recent posts of different categories. On clicking category 1, I want to display the recent posts from cat1 and on clicking cat 2 the recent posts from cat2 and so on.

The code I have written was displaying the same posts on all cats.

What should I do?

$rPosts = new WP_Query();

while ($rPosts->have_posts()) : $rPosts->the_post(); ?>

    <h4><a href="#">POSTS FROM THIS CAT</a></h4>
    <p><?php the_post_thumbnail(array(300,221)); ?> </p>
    <h3><a href="#"><?php echo get_the_title();?></a></h3>
    <p><a class="btn btn-white" href="<?php the_permalink(); ?>">Read Full Story</a></p>


1 Answer 1


You're always displaying the posts from the category "category1" because that's what you've written in your code here $rPosts->query('showposts=4&category_name=category1');

If you want to just go to the category archive pages you can do that by generating the links with get_term_link. This function goes at the position where you're displaying the links & then this code is not needed. If not that please give us more details about where you're displaying the links & how exactly you want it to work

As a side note "showposts" is deprecated, please use "posts_per_page" instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.