WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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>

share|improve this question

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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