Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have three columns, each displaying 2 posts, each from different categories and each being a new query.

These three columns are shown on thier parent category page, which is set to display 6 posts per page.

$soundPosts = new WP_Query('posts_per_page=2&cat=4&paged=' . get_query_var( 'paged' ));
$viewsPosts = new WP_Query('posts_per_page=2&cat=5&paged=' . get_query_var( 'paged' ));
$wordsPosts = new WP_Query('posts_per_page=2&cat=6&paged=' . get_query_var( 'paged' ));

At the moment pagination kind of works, but not properly. It is using the pagination for the main parent category.

        <div class="navigation">
            <div class="left"><?php previous_posts_link('&#60; Previous') ?></div>
            <div class="right"><?php next_posts_link('Next &#62;','') ?></div>
        </div>

The pagination kind of works, as the parent category contains the posts, and is set to show 6 posts, but it doesn't always work as the posts aren't always equally distributed throughout the columns.

How can I manually build the navigation links, so they run off the queries above, rather than the archive's default pagination? And how and the end of all records can I make the archive return a 404 page, for use with infinte scroll.

Also of note, I am aware of this question / answer, but don't think it is an accpetable solution, or is in fact the same setup I have currently.

Thanks alot for any pointers!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

OK this is what I ended up with, that works well:

// get paged value.
$paged = get_query_var( 'paged' );
$maxPages = array();
$max = 0;

// Create the queries
$soundPosts = new WP_Query('posts_per_page=1&cat=4&paged=' . $paged);
$viewsPosts = new WP_Query('posts_per_page=1&cat=5&paged=' . $paged);
$wordsPosts = new WP_Query('posts_per_page=1&cat=6&paged=' . $paged);

// get max number of pages for each query
array_push($maxPages,$soundPosts->max_num_pages);
array_push($maxPages,$viewsPosts->max_num_pages);
array_push($maxPages,$wordsPosts->max_num_pages);

// get the max number of pages from all three.
$max = max($maxPages); 

// If we are out of posts, return 404
if ($paged > $max) {
$wp_query->set_404();
status_header('404');
}

// proceed as normal.
get_header(); 

Then for the pagination links, just a bit of logic from the variables created to decide whether to show the links or not.

<?php 
if ($paged > 0 && $paged < $max) { ?>
    <div class="left"><?php previous_posts_link('&#60; Previous') ?></div>
<?php 
}
if ($paged <= $max) { ?>
    <div class="right"><?php next_posts_link('Next &#62;','') ?></div>
<?php } ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.