0

What I need is one post (feature image+content text) per page. When you click on a fullscreen div (class=next)on top of everything, then next post loads per page. When the oldest and last post appears, clicking on its feature image loads the first post again (latest post added by date). One post per page. Infinite cycle of posts by adding date, starting with the latest post added.

Below is the code with two problems to solve.

  1. Oldest post does not cycle back to the first post, to create an infinite cycle of posts.
  2. The index (latest post )loads all content from all posts, all the next posts are fine, containing only their content.

    <a href="<?php echo get_permalink( get_adjacent_post( false, '', true ) ); ?>">
    <span class="next"></span>
    <div class="featured-image" style="background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>')"></div></a>
    <div class="content">
    <?php $query = new WP_Query( array( 'posts_per_page' => '1', 'order' => 'DESC', 'orderby' => 'date' ) ); while($query->have_posts()){ $query->the_post(); } ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?><?php the_content(); ?>
    <?php endwhile; else: ?>
    <?php _e(''); ?><?php endif; ?>
    </div>
    

Need help! Thanks!

S

1 Answer 1

0

It should be more simple. Like this:

<?php
$next_post = get_previous_post();
if( empty( $next_post ) ) {
    $posts = get_posts( array( 'posts_per_page' => 1 ) );
    $next_post = $posts[0];
}
?>

<a href="<?php echo get_permalink( $next_post->ID ); ?>">
    <span class="next"></span>
    <div class="featured-image" style="background-image: url('<?php echo wp_get_attachment_url( get_post_thumbnail_id( $next_post->ID ) ); ?>')"></div>
</a>
<div class="content">
    <?php
    echo apply_filters( 'the_content', get_post_field( 'post_content', $next_post->ID ) );
    ?>
</div>
7
  • Thanks Sohan. I get error: Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) for this line: $posts = get_post( 'posts_per_page' => 1 ); I tried '1' but no help. S
    – sizer
    Nov 1, 2016 at 10:21
  • My bad.. Just fixed the code. Please use the latest edit. Nov 1, 2016 at 11:05
  • Much better, but still the cycle is not complete. It will not connect the oldest and newest post to create an infinite cycle of posts. The linking order is also oldest to newest, I need newest to oldest and then back to the newest post. S
    – sizer
    Nov 1, 2016 at 12:28
  • In that case you just have to replace get_next_post(); with get_previous_post();. I'll edit that for you. Nov 1, 2016 at 17:11
  • Thank You so much Sohan! That makes a perfect post loop, but it starts with the second latest post. How can I make it start with the latest post added, not the second to the latest (by date)?
    – sizer
    Nov 3, 2016 at 9:56

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.