0

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.

1

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 );
        }
    }
}

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.