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 need to show more post at the category state (with tag_ID=21), for example, I set the number of post show at homepage is 6, but I want 10 post when archive state, is there any way to add a filter in functions.php?

Maybe posts_per_page and wp count posts is helpful.

---Edit---

My Code in functions.php is:

function change_numberposts( $query ) {
    // Only modify the main query
    if( $query->is_main_query() ){
        // Category 21
        if ( $query->is_category( array( 21, 'shuoshuo', '说说' )  ) ) {
            $query->set( 'posts_per_page', 10 );
        } else {
        $query->set( 'posts_per_page', 6 );     
        }
    }
}

add_action( 'pre_get_posts', 'change_numberposts' );

I also tried

is_category( 21 )
is_category('21')
is_category('shuoshuo')

The only work case is

is_category()

which will output 10 posts in each category.

I also noted that is_category( 21 ) is work for the permalinks set to be numeric. Since my permalinks is self-defined: /%year%/%monthnum%/%day%/%postname%.html, how to make the above code behaved as expected?

share|improve this question

1 Answer 1

put this code in functions.php :

function change_numberposts( $query ) {
    // Only modify the main query
    if( $query->is_main_query() ){
        // Home page
        if ( $query->is_home() ) {
            $query->set( 'posts_per_page', 6 );
        }
        // Category 21
        if ( $query->is_category( 21 ) ) { // parameter for is_category may be Category ID, Category Title, Category Slug or Array of IDs, names, and slugs.
            $query->set( 'posts_per_page', 10 );
        }
    }
}
add_action( 'pre_get_posts', 'change_numberposts' );

Links:

is_category

share|improve this answer
    
You should put all conditional logic inside the callback, for clarity. –  Chip Bennett Nov 27 '13 at 13:22
    
I see what you mean. I usually put the conditionnal before calling the function, but your way allow to add further rules later more easily. Thanks for editing, Chip Bennett. –  Simon Nov 27 '13 at 13:26
1  
Also: the query conditionals won't likely be available at plugins_loaded, which is when your add_action() conditional wrapper would fire by default. –  Chip Bennett Nov 27 '13 at 13:41
    
That almost work, but since my permalinks set to: /%year%/%monthnum%/%day%/%postname%.html, which is not work, why? –  van abel Nov 27 '13 at 14:43
    
Don't know, how does it does not work ? What's happening ? –  Simon Nov 27 '13 at 15:32

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.