I am trying to set up a category archive (editing category.php) that displays a list of posts from one single category. If I left the twentyten default code

(get_template_part( 'loop', 'category' );)

and i go to www.mysite.com/categoryname it correctly filters the posts only for the categoryname.

If I try to use my custom query code, going to www.mysite.com/categoryname every post is displayed, despite of category. This is the loop code:

    <?php if (have_posts()) : ?>
               $args = array(
                   'post_type' => 'post',
                   'posts_per_page' => 5,
                   'orderby' => comment_count,
        while (have_posts()) : the_post();?>


   <?php endwhile; ?>           
  <?php else : ?>
 <?php endif; ?>



That happens because you are overwriting the query with you $args, If you want to modify it and not overwrite it then use this format:

//get the $query_string in to your $args array
global $query_string;
parse_str( $query_string, $args );
//modify whatever you want
$args['post_type'] = 'post';
$args['posts_per_page'] = 5;
$args['orderby'] = 'comment_count';
  • Thanks, but I modified as above and the result is the same. In my custom query I need to dynamically specify which cat_name must be displayed (the same as /categoryname). (I am not using the theme loop). For sure I'm missing something! – Andycap May 7 '11 at 13:28
  • 1
    if you are on a category page then the category should be in the query already, if not then add $args['cat'] = get_query_var('cat'); – Bainternet May 7 '11 at 13:54
  • Yeah!! I did a typo, was working also your first solution without the $args['cat']! Thank you! – Andycap May 7 '11 at 14:07
  • Glad i could help – Bainternet May 7 '11 at 14:47

protected by Community Apr 26 at 4:40

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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