3

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.

6

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.

| improve this answer | |
  • I used the the 2nd solution and it worked. Thanks – Ahmad Feb 1 '12 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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