Question

according with Pagination with custom loop. I use the custom loop for display flash game. For make a pagination on the page with posts from one category (mydomain/category/categoryName) I used:

add_action( 'pre_get_posts', 'wpse5477_pre_get_posts' );
function wpse5477_pre_get_posts( &$wp_query )
{
    if ( $wp_query->is_category() ) {
        $wp_query->set( 'post_type', 'game' );
        $wp_query->set( 'posts_per_page', 9 );
    }
}

I have the section on the main page of my site, where displayed three game from each category. But according with code above I can't display only 3 games, even if I determine in array('post_per_page', 3) or smth like this, because this number have been already determine in $wp-query. how could I kill two birds with one stone? Thanks.

Was it helpful?

Solution

You can check for the existence of a variable, so you don't overwrite it:

add_action( 'pre_get_posts', 'wpse7262_pre_get_posts' );
function wpse7262_pre_get_posts( &$wp_query )
{
    if ( $wp_query->is_category() ) {
        if ( ! array_key_exists( 'post_type', $wp_query->query_vars ) ) {
            $wp_query->set( 'post_type', 'game' );
        }
        if ( ! array_key_exists( 'posts_per_page', $wp_query->query_vars ) ) {
            $wp_query->set( 'posts_per_page', 9 );
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top