Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Home.php issue - posts just repeat on 'Next Page' WordPress

  • SOLVED

I've got a weird problem -- my blog page just repeats all of the same posts when the next button is clicked. Everything else seems fine but that. Can anyone give me an idea as to why?

*Note, the php if stuff is just putting a temp image in place if they don't upload one. I'm using Advanced Custom Fields for that.




<?php if ( have_posts() ) : ?>

<?php
$args = array( 'posts_per_page' => 9, 'order'=> 'post_date', 'orderby' => 'post_date', 'post__not_in' => $sticky );
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<div class="blog-post" itemscope itemtype="http://schema.org/Blog">

<?php if (get_field('home_image')) { ?>

<div class="blog-image"><a href="<?php the_permalink(); ?>">
<?php
$att_id = get_field('home_image');
$img_array = wp_get_attachment_image_src( $att_id, 'full');
$img_url = $img_array[0];
?>
<img src="<?php echo $img_url; ?>">
</a></div>

<?php } else { ?>

<div class="blog-image"><a href="<?php the_permalink(); ?>"><img src="<?php home_url(); ?>/media/blog-filler.png" alt="<?php the_title(); ?>"></a></div>

<?php } ?>

<div class="home-blog-date-strip" style="background:<?php the_field('blog_date_background',2); ?>">
<div class="home-blog-clock"><img src="<?php home_url(); ?>/media/home-blog-clock.png" alt="<?php echo get_bloginfo('name'); ?> Blog Post"></div>
<div class="home-blog-date"><a style="color:#fff;" href="<?php the_permalink(); ?>"><span itemprop="dateCreated"><?php the_date(); ?></a></span></div>
<div class="clear"></div>
</div>

<div class="blog-text-area box-sizing" style="background:<?php the_field('blog_text_background',2); ?>">
<div class="home-post-title" style="<?php the_field('blog_title_font_family',2); ?>;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" style="color:#000;"><span itemprop="about"><?php the_title(); ?></a></span></div>
<div class="home-post-text"><span itemprop="blogPost"><?php the_excerpt(); ?></span></div>
</div>
</div>
<?php
endforeach;
wp_reset_postdata();
?>

</div>
</div>

<?php wp_link_pages(array('before' => '<nav class="pagination">', 'after' => '</nav>')); ?>
<?php endif; ?>


Answers (1)

2015-07-31

Darlene Grace Arcenal answers:

Add this on your args 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )


Darlene Grace Arcenal comments:

It will now be:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 9, 'order'=> 'post_date', 'orderby' => 'post_date', 'post__not_in' => $sticky, 'paged' => $paged );


Kyler Boudreau comments:

Thanks Darlene,

When I add that, it completely removes the bottom navigation.


Darlene Grace Arcenal comments:

What if you use wp_query instead of get_posts

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 9, 'order'=> 'post_date', 'orderby' => 'post_date', 'post__not_in' => $sticky, 'paged' => $paged );
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

<!-- pagination here -->

<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog-post" itemscope itemtype="http://schema.org/Blog">



<?php if (get_field('home_image')) { ?>



<div class="blog-image"><a href="<?php the_permalink(); ?>">

<?php

$att_id = get_field('home_image');

$img_array = wp_get_attachment_image_src( $att_id, 'full');

$img_url = $img_array[0];

?>

<img src="<?php echo $img_url; ?>">

</a></div>



<?php } else { ?>



<div class="blog-image"><a href="<?php the_permalink(); ?>"><img src="<?php home_url(); ?>/media/blog-filler.png" alt="<?php the_title(); ?>"></a></div>



<?php } ?>



<div class="home-blog-date-strip" style="background:<?php the_field('blog_date_background',2); ?>">

<div class="home-blog-clock"><img src="<?php home_url(); ?>/media/home-blog-clock.png" alt="<?php echo get_bloginfo('name'); ?> Blog Post"></div>

<div class="home-blog-date"><a style="color:#fff;" href="<?php the_permalink(); ?>"><span itemprop="dateCreated"><?php the_date(); ?></a></span></div>

<div class="clear"></div>

</div>



<div class="blog-text-area box-sizing" style="background:<?php the_field('blog_text_background',2); ?>">

<div class="home-post-title" style="<?php the_field('blog_title_font_family',2); ?>;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" style="color:#000;"><span itemprop="about"><?php the_title(); ?></a></span></div>

<div class="home-post-text"><span itemprop="blogPost"><?php the_excerpt(); ?></span></div>

</div>

</div>
<?php endwhile; ?>
<!-- end of the loop -->
<?php posts_nav_link(' — ', __('&laquo; Newer Posts'), __('Older Posts &raquo;')); ?>
<!-- pagination here -->

<?php wp_reset_postdata(); ?>

<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>


Kyler Boudreau comments:

That displays the posts correctly, but there is no navigation to see the other posts. Weird.


Kyler Boudreau comments:

Actually, BOOM. I had something wrong. Your code worked! Thank you.