Question

I'm using this script on my archive.php in order to list a link list of all posts contained in the current category. However, its limiting the result count relative to the number assigned to "Settings > Reading > Blog Pages Show At Most _ posts".

In other words, there may be 10 posts that are in this category, but its only displaying up to the number assigned in that setting.

How can I change my script to ignore this setting?

<?php if(is_category()) while (have_posts()) : the_post(); ?>
    <li id="post-<?php the_ID(); ?>">
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
        <?php the_title(); ?>
        </a> 
        <?php echo get_link_excerpt(); ?>
    </li>
<?php endwhile; } ?>
Was it helpful?

Solution

Instead of using the default loop, perhaps try building your own query.

Basically, it'll look something like this:

$your_posts = get_posts('cat=123&posts_per_page=123');
foreach ($your_post as $post) {
   do_something-with($post);
}

You can display all posts by either use the param 'posts_per_page'=>-1 or nopaging=true.

(Not sure why someone voted this down? IMHO said person should comment on how the question should be improved if she or he is going to do that.)

OTHER TIPS

To modify the posts_per_page setting on a contextual basis, the best approach is to filter the query via callback to pre_get_posts. For example, to set posts_per_page to 10 for a specific category (say, category ID 7):

function wpse8982_filter_pre_get_posts( $query ) {
    if ( $query->is_main_query() && is_category( 7 ) ) {
        $query->set( 'posts_per_page', '10' );
    }
}
add_action( 'pre_get_posts', 'wpse8982_filter_pre_get_posts' );

Note that you can pass any valid setting for posts_per_page.

If you want to do something other than the automagical defaults allow for, you need to take control of your Loop. Read the docs on The Loop and take particular notice of the section labeled Multiple Loops. It shows alternate ways of setting up the query and displaying things. You should also take a look at WP_Query as that is key to what is going on here.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top