1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem with pagination

Discussion in 'PHP' started by Divvy, Jun 28, 2017.

  1. #1
    Hello guys,

    Can someone give me a little help here?
    SEMrush
    I have this php code:
    
    <?
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $query_args = array(
      'post_type' => 'vrvideo',
      'posts_per_page' => 5,
      'paged' => $paged
    );
    
    $loop = new WP_Query( $query_args );
    echo '<div class="crp_related "><ul>';
    
    while ( $loop->have_posts() ) : $loop->the_post();
    echo '<li><a href="', the_permalink(), '">', the_post_thumbnail('related-thumb'), '</a>';
    echo '<span class="crp_title" style="bottom:4px;">';
    if (strlen($post->post_title) > 27) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 27) . '...'; }
    else {
    the_title();
    }
    echo '</span></li>';
    endwhile;
    
    echo '</ul>';
    echo '<div class="crp_clear"></div></div>';
    ?>
    
    
    <?php if ($loop->max_num_pages > 1) { // check if the max number of pages is greater than 1  ?>
      <nav class="prev-next-posts">
        <div class="prev-posts-link">
          <?php echo get_next_posts_link( 'Older Entries', $loop->max_num_pages ); // display older posts link ?>
        </div>
        <div class="next-posts-link">
          <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
        </div>
      </nav>
    <?php } ?>
    
    <?php wp_reset_postdata(); ?>
    
    PHP:
    In my page, I have 6 posts.
    The pagination appears fine and is showing 5 posts. Everything good here.
    The problem is that when I change page, is showing always the same 5 posts in all pages.

    What I did wrong?
    Could be the $loop->max_num_pages?

    HELP :)
     
    Divvy, Jun 28, 2017 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Long time since I worked with pagination on WP, but shouldn't the next/prev links be swapped?
     
    PoPSiCLe, Jun 28, 2017 IP
  3. Divvy

    Divvy Well-Known Member

    Messages:
    727
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #3
    Hey bud :)

    What do you mean?
    Can you give me an example?
     
    Divvy, Jun 28, 2017 IP
  4. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #4
    Does it happen if you press both links or just for the one that's supposed to give you older posts?

    P.S. Your prev-posts-link class includes next posts and vice versa for the second one. Fix that first to avoid confusion.
     
    Blank ™, Jun 28, 2017 IP
  5. Divvy

    Divvy Well-Known Member

    Messages:
    727
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #5
    Blank, here's an example:

    Page with 6 posts.
    When you enter in the page, appears 5 results (post1,post2,post3,post4,post5) and pagination below.
    When you click in Older Entries, appears exactly same 5 results (post1,post2,post3,post4,post5) instead of post6.
    And if you click in Older Entries again (page 3) appears exactly same results again... and so on...

    I tried to replace the code from:
    
    <?php if ($loop->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
    <nav class="prev-next-posts">
    <div class="prev-posts-link">
    <?php echo get_next_posts_link( 'Older Entries', $loop->max_num_pages ); // display older posts link ?>
    </div>
    <div class="next-posts-link">
    <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
    </div>
    </nav>
    <?php } ?>
    PHP:
    To:
    
    <?php
    // next_posts_link() usage with max_num_pages
    next_posts_link( 'Older Entries', $loop->max_num_pages );
    previous_posts_link( 'Newer Entries' );
    ?>
    
    PHP:
    Works exactly with same issue, so probably the issue is in the code above.
     
    Divvy, Jun 28, 2017 IP
  6. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #6
    
    <?php if ($loop->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
    <nav class="prev-next-posts">
    <div class="prev-posts-link">
    <?php echo get_previous_posts_link( 'Older Entries', $loop->max_num_pages ); // display older posts link ?>
    </div>
    <div class="next-posts-link">
    <?php echo get_next_posts_link( 'Newer Entries' ); // display newer posts link ?>
    </div>
    </nav>
    <?php } ?>
    PHP:
    And this?
     
    Blank ™, Jun 28, 2017 IP
  7. Divvy

    Divvy Well-Known Member

    Messages:
    727
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #7
    With that one the pagination doesn't appear.
     
    Divvy, Jun 28, 2017 IP
  8. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #8
    What? :D I just swapped links, that's all. If it breaks there, there must be something completely wrong in the core, not the given snippet.

    What is $loop->max_num_pages returning (just echo it)?
     
    Blank ™, Jun 28, 2017 IP
  9. Divvy

    Divvy Well-Known Member

    Messages:
    727
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #9
    Yeah that's weird hehe

    Here goes the echo of:
    <?php echo $loop->max_num_pages; ?>
    PHP:
    Returning:
    2
    Code (markup):
    With this code only appears 2 pages as should be:
    
    <?php
    // next_posts_link() usage with max_num_pages
    next_posts_link( 'Older Entries', $loop->max_num_pages );
    previous_posts_link( 'Newer Entries' );
    ?>
    PHP:
    The problem is that is always showing same results in 2nd page...

    The issue must be here somewhere:
    
    $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
    $query_args = array(
    'post_type' => 'vrvideo',
    'posts_per_page' => 5,
    'paged' => $paged
    );
    
    $loop = new WP_Query( $query_args );
    PHP:
     
    Divvy, Jun 28, 2017 IP
  10. Blank ™

    Blank ™ Well-Known Member

    Messages:
    223
    Likes Received:
    18
    Best Answers:
    6
    Trophy Points:
    110
    #10
    Try adding wp_reset_query() after all of the queries called. That's all I can think of w/o having access to given files/website (feel free to PM me though if you are open to share those details; no cost whatsover).
     
    Blank ™, Jun 28, 2017 IP
  11. Divvy

    Divvy Well-Known Member

    Messages:
    727
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #11
    Hey,

    I solved the problem :D

    I replaced this line:
    'paged' => $paged
    PHP:
    With this one:
    'paged' => get_query_var( 'paged' )
    PHP:
    Now is working perfectly.

    Thank you anyway for trying to help me :)
     
    Divvy, Jun 28, 2017 IP
    Blank ™ likes this.
  12. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #12
    The reason that worked was that you used the wrong name for the call in the get_query_var() when you assigned it to the $paged-variable - you used 'page' inside those, but you used 'paged' in the one that works...
     
    PoPSiCLe, Jun 30, 2017 IP