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 using the twenty eleven theme in my wordpress site.

In posts, I have two categories. One is blogs and other is News.

The monthly archive displays the posts of all categories while I want that the monthly archive should only display the posts of blogs category.

How will I do this?

I have also installed the wp_pagenavi plugin for the pagination.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

There are two ways of doing it:

You can use a filter to alter the query when viewing an archive page. You will need to find the ID of your category 'blogs' (you can obtain it from the slug using get_term_by). Alternatively you can exclude a particular category by ID.

add_action( 'pre_get_posts', 'my_change_query'); 
    function my_change_query($query){
        if(is_archive()){
           $blog_term = get_term_by('slug', 'blogs', 'category');
           $blog_term_id = $blog_term->term_id;
           $query->set('cat', $blog_term_id);//Include category with ID $blog_term_id
           //$query->set('cat','-'.$blog_term_id);//Exclude category with ID  $blog_term_id
        }  
     return $query
    };

or, more commonly, you can alter the archive.php template file and insert the following just above the if(have_posts()).

global $wp_query;
$args = array_merge( $wp_query->query, array( 'category_name' => 'blogs' ) );
query_posts( $args );

See the Codex on query_posts and WP_Query.

share|improve this answer
    
I used the the 2nd solution and it worked. Thanks –  Ahmad Feb 1 '12 at 11:55
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.