0

When I click on "older entries" in my home page, it goes to a second link but it shows the same posts. I think I need to modify something in the index.php, this is the full code section that I think I need to modify:

<div class="container">
    <div class="post_content">
        <div class="home_posts">
            <?php
                $args2 = array(
                'post_type' => 'post',
                'posts_per_page' => 10,
                'paged' => ( get_query_var('paged') ? get_query_var('paged') : 2),
                );
                $query = new WP_Query( $args2 );
                if ( $query->have_posts() ) :
                    while ( $query->have_posts() ) : $query->the_post();
                        echo '<div class="grid_post">
                                <h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>';
                        $type = get_post_meta($post->ID,'page_featured_type',true);
                        switch ($type) {
                            case 'youtube':
                                echo '<iframe width="560" height="315" src="http://www.youtube.com/embed/'.get_post_meta( get_the_ID(), 'page_video_id', true ).'?wmode=transparent" frameborder="0" allowfullscreen></iframe>';
                                break;
                            case 'vimeo':
                                echo '<iframe src="http://player.vimeo.com/video/'.get_post_meta( get_the_ID(), 'page_video_id', true ).'?title=0&amp;byline=0&amp;portrait=0&amp;color=03b3fc" width="500" height="338" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
                                break;
                            default:
                                echo '<div class="grid_post_img">
                                            <a href="'.get_permalink().'">'.get_the_post_thumbnail().'</a>
                                        </div>';
                                break;
                        }
                        echo '<div class="grid_home_posts">
                                    <p>'.dess_get_excerpt(120).'</p>
                                </div>
                            </div>
                            ';
                    endwhile;
            ?>
    </div>
    <?php
        echo '<div class="load_more_content"><div class="load_more_text">';
                ob_start();
                    next_posts_link('LOAD MORE',$query->max_num_pages);
                    $buffer = ob_get_contents();
                ob_end_clean();
                if(!empty($buffer)) echo $buffer;
            echo'</div></div>';                 
        $max_pages = $query->max_num_pages;
            wp_reset_postdata();
        endif;
    ?>
    <span id="max-pages" style="display:none"><?php echo $max_pages ?></span>
    </div>
    <?php get_sidebar(); ?>
    <div class="clear"></div>
</div>
</div>

Could it be here:

<?php


$args2 = array(
    'post_type' => 'post',
    'posts_per_page' => 6,
    'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
    );
    $query = new WP_Query( $args2 );
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post();
            echo '<div class="grid_post">

This is the URL to "Older entries": http://www.wha2wear.com/page/2/ that shows the same content as the homepage. Also, I don't really see that "Older entries" is written somewhere in the code..

Also, in the actual page, Front Page page, there is this code

[posts-for-page order_by ='date' hide_images='false' num='11' read_more='
Read More »' show_full_posts='false' use_wp_excerpt='true' strip_html='true' hide_post_content='false' show_meta='false' force_image_height='200' force_image_width='250']

I have the feeling that this code has higher priority than the php, so the homepage does what it says there?

  • 1
    Try changing your 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 2), to 'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1; IT seems that the issue might because of the get_query_var('paged') you have by default set it up as 2. Try changing the value from 2 to 1 then that should solve your pagination issue – bagpipper Jul 18 '16 at 8:06
  • 3
    Do not use a custom query, that is your biggest issue here. – Pieter Goosen Jul 18 '16 at 9:27
  • thank you, but I still have the same problem, it goes to wha2wear.com/page/2 and same posts appear :( what else would be? thanks a lot – Wha2wear Jul 18 '16 at 16:41
  • Hi guys, I've edited my question because I think I need to modify the full php query (the second bit of code), could you tell me how it should be, please? – Wha2wear Jul 19 '16 at 5:20
  • 3
    As I stated, do not use a custom query, see this post – Pieter Goosen Jul 19 '16 at 5:58
1

Why don't you try with default query and use like this

if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }

$args = array(
'post_type'         => 'post',
'posts_per_page'    => 6,
'paged'             => $paged,
);

query_posts($args); while (have_posts()): the_post();

// do something

endwhile;

See here more details about Adding the "paged" parameter to a query

https://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

  • thank you, but I put that but still see the same. Also, the homepage page in wordpress have some code. I'll edit my question in case it's something to do with that – Wha2wear Jul 22 '16 at 9:35
  • please, have a look to the info I added to my question. – Wha2wear Jul 22 '16 at 9:39
  • When you are using query_posts then did you change next_posts_link() parameter ? anyway you can try just next_posts_link(''LOAD MORE') without using $max_pages parameter or next_posts_link(''LOAD MORE', 6) static next_posts_link(''LOAD MORE') parameter . Once a time I faced this problem and then I solved this with front-page-wp-query-pagination-issue-repeating-posts-next_posts_link-help this posts solution that I already share with you :) – Mohammad Limon Jul 22 '16 at 15:25
  • thank you. where should I put the next_posts_link(''LOAD MORE', 6) parameter? Is that inside the query where you put // do something? – Wha2wear Jul 23 '16 at 15:29
  • It will work inside and outside as well :) – Mohammad Limon Jul 24 '16 at 16:11
0

If the posts are being displayed via a static front page (they seem to be?) then you need to use the query parameter page not paged. You could try this:

$args2 = array(
    'post_type' => 'post',
    'posts_per_page' => 6,
    'paged' => ( get_query_var('page') ? get_query_var('page') : 1),
);

As mentioned on: https://codex.wordpress.org/Function_Reference/get_query_var

Edit: note however the paged parameter is still set in the query array but the page query_var is used. This from example at: https://codex.wordpress.org/Function_Reference/WP_Query

  • thanks a lot, I cleared the cache and I put the php to debug at the beginning. I also put the the code from your answer, but I don't know why I still see the same (including the 10 posts as before) and I delete my browser cache too. What do you see? – Wha2wear Jul 22 '16 at 9:08
  • I see nothing, it surely must be the wrong file you are editing, index.php is a fallback when no other template is being used. – majick Jul 22 '16 at 9:12
  • please, have a look to the info I added to my question. – Wha2wear Jul 22 '16 at 9:39
  • looks like you are onto it with ajax load more... that is a better way to go here... since you can customize the repeater template and the replace the existing shortcode you have with the ALM shortcode to show the initial posts. – majick Jul 22 '16 at 14:24
  • I know, that's what I did, but I'd like also to delete the "Older entries" text. Do you know how I can find it? – Wha2wear Jul 23 '16 at 15:27
0
+100
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
    'orderby'=> 'id',
    'order' => 'desc',
    'paged'=>$paged
  );
global $wp_query;
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $args );
  if ( have_posts() ) : 

    while ( have_posts() ):
       the_post();  


      get_template_part( 'template-parts/content', get_post_format());
      endif;

      $count++;
      endwhile;
      // If no content, include the "No posts found" template.
    else :
        get_template_part( 'template-parts/content', 'none' );

    endif;

         $nav = get_the_posts_pagination( array(
            'mid_size'           => 4,
            'prev_text'          => __( '&laquo;', 'twentysixteen' ),
            'next_text'          => __( '&raquo;', 'twentysixteen' ),
            'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( '', 'twentysixteen' ) . ' </span>',
            'screen_reader_text' => __( 'Post Navigation' )
        ) );

         $nav = str_replace('<h2 class="screen-reader-text">Post Navigation</h2>', '', $nav);
         echo $nav;

    $wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();$wp_query = null;
    $wp_query = $original_query;
    wp_reset_postdata();

this is how it will work, you need to update WP_Query, not to use another object, it wouldn't work with pagination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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