0

I know this has been discussed before but I can't seem to make it work with any of the solution posted in other topics.

How can I make pagination to work on a loop like this:

        <div id="first-loop-container">
        <?php $my_query = new WP_Query('showposts=5'); ?>
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h4>" title="<?php the_title(); ?>"><?php the_title(); ?></h4>
        <?php the_content(" More...",TRUE,''); ?>
        <?php endwhile; ?>
        </div>

        <div id="second-loop-container">
        <?php $my_query = new WP_Query('showposts=5&offset=5'); ?>
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h4>" title="<?php the_title(); ?>"><?php the_title(); ?></h4>
        <?php the_content(" More...",TRUE,''); ?>
        <?php endwhile; ?>
        </div>


<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'prev_text' => __('« Previous'),
    'next_text' => __('Next »'),
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>

I want first page to display 10 unique posts (5 in first loop and 5 in the second), second page the next 10 unique posts ( same as before) and so on.

Thank you.

UPDATED : Added pagination.

2
  • 1
    Your code does not demonstrate any attempt at getting pagination to work. What have you tried exactly?
    – s_ha_dum
    Nov 24, 2012 at 22:59
  • Ah sorry , you are right. I updated my first post. Thank you.
    – Victor
    Nov 25, 2012 at 10:36

2 Answers 2

1

You have to pass pagination parameters to your queries as well. As it is, you are just pulling 5 posts. By default that is going to be the most recent five. Then another query pulls the next five. The fact that you are trying to paginate two loops makes this trickier than normal. However...

At first at first I thought you were pulling two different post types or categories, but it looks like you are just creating two loops of the same type-- there is no parameter that would do anything different. You can simplify this to a single query and make pagination easier.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query( 
    array(
        'showposts'=>10,
        'paged'    => $paged 
    )
); 

?>
<div id="first-loop-container">
    <?php 
    $i = 0;
    while (5 >= $i) : 
        $my_query->the_post(); ?>
        <h4 title="<?php the_title(); ?>"><?php the_title(); ?></h4>
        <?php the_content(" More...",TRUE,''); 
        $i++; ?>
    <?php endwhile; ?>
</div>
<div id="second-loop-container">
    <?php 
    $i = 0;
    while ($my_query->have_posts()) : 
        $my_query->the_post(); ?>
        <h4 title="<?php the_title(); ?>"><?php the_title(); ?></h4>
        <?php the_content(" More...",TRUE,''); 
        $i++; ?>
    <?php endwhile; ?>
</div><?php

$big = 999999999; // need an unlikely integer

  echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'prev_text' => __('« Previous'),
        'next_text' => __('Next »'),
        'current' => max( 1, get_query_var('paged') ),
        'total' => $my_query->max_num_pages
    ) );

You start off grabbing your pagination parameters and saving them to $paged. You use that in your single query. Next you run through the first five posts in the query results, and then the second five. You can do anything you want between the two so long as you don't clobber or reset $my_query. Then your pagination. You were trying to use the global $wp_query, which was wrong since you are trying to paginate $my_query, so I changed that. I also corrected some markup with your <h4> tag,

See if that works better.

0

Thank you very much for you answer.

For some reason, maybe I implemented you code wrong in my page, it didn't work. It created a long infinite page in which it replicated infinitely my first posts.

But luckily I combined some code from different tutorials regarding this issue and I came up with this one which worked for me:

    <div id="first-loop-container">
<?php if (have_posts()) : ?>
<?php $count = 0; ?>

<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>

<?php if ($count <= 5) : ?>
<h4 title="<?php the_title(); ?>"><?php the_title(); ?></h4>
        <?php the_content(" More...",TRUE,''); 
    <?php endif; endwhile; ?>
</div>


<div id="second-loop-container">
<?php rewind_posts();
$cnt = 0;
while (have_posts()) : the_post(); if ($cnt >= 5 ): ?>
<h4 title="<?php the_title(); ?>"><?php the_title(); ?></h4>
        <?php the_content(" More...",TRUE,''); 
<?php endif; $cnt++; endwhile; ?>

</div>

<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'prev_text' => __('&laquo; Inapoi'),
    'next_text' => __('Urmatoarea &raquo;'),
    'current' => max( 1, get_query_var('paged') ),
    'total' => $wp_query->max_num_pages
) );
?>
<?php else : ?>
<?php endif; ?>

I only hope my code is valid but as long as it does the job...

Anyway thank you again for your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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