Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

 <?php  
$rPosts = new WP_Query();
$rPosts->query('showposts=4&category_name=category1');

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>

<?php 
endwhile; 
?>
share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.