• Resolved hberger

    (@hberger)


    Hi,

    I want to show different number of posts per page depending on which category I am viewing.

    I did try this:

    <?php if(is_category('4')) { query_posts('posts_per_page=2');
    } else if(is_category('1')) { query_posts('posts_per_page=3');
    } else if(is_category('5')) { query_posts('posts_per_page=100');
    } else if(is_category('6')) { query_posts('posts_per_page=100');
    } else if(is_category('7')) { query_posts('posts_per_page=100');
    }
    ?>

    I put that code in category.php before:
    <?php if (have_posts()) : ?>

    This results in showing the correct number of posts, but from all categories.
    Am I doing something wrong?

Viewing 13 replies - 1 through 13 (of 13 total)
  • As soon as you declare any parameters in the query_posts line like above you lose any existing queried vars.

    Try this instead, so you preserve the existing query variables.

    <?php
    // 0 is equal to false
    $numposts = 0;
    
    if( is_category(4) ) {
    	$numposts = 2;
    }
    elseif( is_category(1) ) {
    	$numposts = 3;
    }
    elseif( is_category(5) ) {
    	$numposts = 100;
    }
    elseif( is_category(6) ) {
    	$numposts = 100;
    }
    elseif( is_category(7) ) {
    	$numposts = 100;
    }
    $args =
    	// If this
    	( $numposts != false )
    	// Then this
    	? array( 'posts_per_page' => $numposts )
    	// Else this
    	: '';
    $args =
    	// If this
    	( $wp_query->query && !empty( $wp_query->query ) && '' != $numposts )
    	// Then this
    	? array_merge( $wp_query->query , $args )
    	// Else this
    	: $args;
    
    query_posts( $args );
    ?>

    Hope that helps.

    Thread Starter hberger

    (@hberger)

    Thanks a lot, unfortunately it did not work.

    The first category in the “if” works out fine.
    But all of the “elseif” statments doesn′t make any difference.

    What might be wrong?

    This results in showing the correct number of posts, but from all categories

    You have not actually told wordpress to only retrieve posts from those categories. You have only said, “if it is this(4,1,5,etc.) category, then display “x” amount of posts.

    Each line should look more like this:
    <?php if(is_category('4')) { query_posts('cat=4&posts_per_page=2');

    and so on…

    He shouldn’t need to, if the page is queried for a particular category, the query should retain that queried var using the example i provided.

    It may just be that file isn’t handling the query you’re expecting it to.

    What file in your theme are you placing this in?

    If you’re putting this in the theme’s index.php for example, but a category.php exists, then the category.php will run the code, not the index.php.

    Sorry just spotted you did mention category.php.

    Do you have template files (theme files) that have a name like..

    category-1.php
    category-4.php

    ..and so on (just examples)…

    As they will be given priority over category.php

    Thread Starter hberger

    (@hberger)

    Hmm.. Thank you racer x for your answer. I changed it to this:

    <?php if(is_category('3')) { query_posts('cat=3&posts_per_page=2');
    } else if(is_category('1')) { query_posts('cat=1&posts_per_page=3');
    } else if(is_category('5')) { query_posts('cat=5&posts_per_page=1');
    } else if(is_category('6')) { query_posts('cat=6&posts_per_page=1');
    } else if(is_category('7')) { query_posts('cat=7&posts_per_page=1');
    }
    ?>

    That makes all categories show the correct number of posts, except category 1.
    It shows all of them. If I change the number “3” to “2” on that row:

    <?php if(is_category('3')) { query_posts('cat=3&posts_per_page=2');
    } else if(is_category('1')) { query_posts('cat=1&posts_per_page=2');
    } else if(is_category('5')) { query_posts('cat=5&posts_per_page=1');
    } else if(is_category('6')) { query_posts('cat=6&posts_per_page=1');
    } else if(is_category('7')) { query_posts('cat=7&posts_per_page=1');
    }
    ?>

    It shows only 2. Isn′t that wierd?

    And, when I hit the “previous posts” is goes to the next page (i can tell from the url), but it shows the same 2 posts.

    Something is wrong..?

    Thread Starter hberger

    (@hberger)

    I copied index.php and named it category.php.

    Am I supposed to create a category.php-file for each category?

    Thread Starter hberger

    (@hberger)

    Oh wait.

    It′s kinda working now. I don′t know why, maybe my cache. Sorry.

    Anyway, it′s showing the correct number of posts, but when i change page to view Older entries, I just get the same posts over and over.

    This is the current code in category.php:

    <?php if(is_category('3')) { query_posts('cat=3&posts_per_page=1');
    } else if(is_category('1')) { query_posts('cat=1&posts_per_page=3');
    } else if(is_category('5')) { query_posts('cat=5&posts_per_page=10');
    } else if(is_category('6')) { query_posts('cat=6&posts_per_page=10');
    } else if(is_category('7')) { query_posts('cat=7&posts_per_page=10');
    }
    ?>

    I see the same posts if I′m at cat=1&paged=2 as if I′m at cat=1&paged=5. Any clues why?

    Like i said before, you’re not preserving any existing query vars, in this case you’re missing the paged variable that handles paging the query results.

    // Example setup of paged var
    $paged = ( get_query_var('paged') && get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
    
    // Example using the var in the query
    query_posts('cat=3&posts_per_page=1&paged='.$paged);

    Thread Starter hberger

    (@hberger)

    Oh, I′m sorry. That made it work, thanks again t31os_! ??

    No problem, you’re welcome..

    Sorry t31os_ I was thinking this was a custom page that he set up, not like an archive.php or categeory-***.php template.

    You are right, the category would have been passed from the query. I also forgot to mention the $paged issue!

    It’s no problem, if the user is happy with the solution he has, then there’s no harm done… ??

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Different posts per page on different categories’ is closed to new replies.