Support » Fixing WordPress » Define posts per page in a Category Template

  • I know that in the WP Admin I can set the number of posts per page as a global variable, but I have a Category template where I’d like to be able to specify the posts per page for only that category (I don’t want to use a widget, I want this hard-coded into the category-11.php category template). Can anyone tell me what that code should look like? I’m sure this can’t be as hard as I’m trying to make it!

    I’m not a PHP wiz, so a complete piece of code would be ideal.

    Thanks a million!

Viewing 8 replies - 1 through 8 (of 8 total)
  • <?php query_posts(‘category_name=News&showposts=100’); ?>
    <?php while (have_posts()) : the_post(); ?>
    <p>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></p>
    <?php endwhile;?>

    One example. Will show whatever category you define for category_name by the name or comma separated category number. Change showposts to amount you want. This will display the title of the posts only. You can always add <?php the_content(); ?> before the endwhile line.

    Thread Starter pshero

    (@pshero)

    Thanks jbrndt. That gets me pointed in the right direction, but it breaks the “next page” link and I’m not sure why.

    I need to make sure that every page in this category displays a maximum of 24 posts.

    My full page code looks like this (without the addition of your code).

    <?php get_header() ?>
    
    		<div id="content">
    
    				<?php while (have_posts()) : the_post(); ?>
    
                    <div class="favorites">
    					<?php //get article_image (custom field) ?>
    					<?php $image = get_post_meta($post->ID, 'article_image', true); ?>
    					<a>" target="_blank" alt="<?php the_title(); ?>">
                        <img src="<?php echo $image; ?>" alt="<?php the_title(); ?>" /></a>
                   		<div class="favorites_link"><a>" target="_blank" alt="<?php the_title(); ?>">
    					<?php the_title(); ?></a></div>
    
                        <?php //get article_summary (custom field) ?>
    					<?php $summary = get_post_meta($post->ID, 'article_summary', true); ?>
    					<?php echo $summary; ?>
    
                   	</div><!-- / #favorites -->
    
                    <!-- end post -->
    
    				<?php endwhile ?>
                    <div style="clear: both;"></div>
    				<!-- USING WP_PAGENAVI PLUGIN PAGE NAVIGATION -->
                    <div id="nav_below" class="navigation">
    				<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
    				</div>
    
    		</div><!-- #content -->
    
    <?php get_sidebar() ?>
    <div style="clear:both;"></div>
    <?php get_footer() ?>

    I see you have a pagenav plugin, but have you tried something like this?

    <?php next_posts_link(‘« Older Entries’) ?>
    <?php previous_posts_link(‘Newer Entries »’) ?>

    You can add that below endwhile, and put a div around each one to align right and left them, etc.

    Thread Starter pshero

    (@pshero)

    jbrndt,
    It doesn’t seem to matter whether I’m using the standard page navigation as you suggest or the plug-in, for some reason in either circumstance when I proceed to page 2 I get a 404 page. The link for page 2 is actually correct when I mouse over the “next page” link, but when it calls the page it throws a 404.

    I’m wondering if somehow the pagination is thrown off by the snippet. ARGH!

    Thread Starter pshero

    (@pshero)

    OK Gang, many thanks to wp_guy for this brilliant work around.

    First, in your category template just above where the loop starts, add the following bit of code. (remember to change the showposts number to the number of posts you wish to display on the page)


    <?php query_posts($query_string."&showposts=2") ?>

    Then in your functions.php page, add this new function to the bottom of the page. (be sure to set the number of posts $perPage to the same number as the one you set in your category template, and make sure that your category id matches right for the $theCategory variable) in the first few lines of the function. *see the comments next to each.


    <?php
    function wpguy_category_limit($limit){
    $perPage = 2; // The number of posts per page
    $theCategory = 11; // The category ID

    $page = $GLOBALS['wp_query']->query_vars['paged'];
    $category = $GLOBALS['wp_query']->query_vars['cat'];
    $is_category = $GLOBALS['wp_query']->is_category;

    if(!$page){
    $page = 1;
    }

    if($is_category == 1 && $category == $theCategory){
    return "LIMIT ".(($page-1)*$perPage).", ".$perPage;
    }

    return $limit;
    }

    add_filter('post_limits', 'wpguy_category_limit');
    ?>

    Thanks again wp_guy! You Rock!

    I’m pretty new at changing code manually (php etc.) So, I need some advice.

    I want to change the amount of posts on my category page(s). I’ve placed the code shown above in my archive.php file but it did not work or change anything. It actually made my post archive stop working…

    Is there a certain place I need to place the code? I don’t have a category.php file, so do I need to create one?

    By the way, I changed my archive posts to titles using the code below. I would assume I would place the code near this code… but again I’m new and a little clueless!!! Any help will be appreciated…

    <?php while (have_posts()) : the_post(); ?>

    <li id=”post-<?php the_ID(); ?>”>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?> <small>- <?php the_time(‘m/d/Y’) ?></small>

    <?php endwhile; ?>

    *The code above was taken from http://www.developedtraffic.com
    **U can check out my site at http://101tees.com/blog/

    I’m pretty new at changing code manually

    Then don’t. Use a plugin: CQS.

    The plugin works like a charm!

    I wish I could have back the time I wasted trying to code everything myself…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Define posts per page in a Category Template’ is closed to new replies.