0

I'm creating my first template.

This is the code. Should be self-explanatory.

<?php
$next_post = get_next_post();
if (!empty( $next_post )): ?>
<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() ); ?>')"></div>
</a>
<?php endif; ?>
<div class="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?><?php the_content(); ?>
<?php endwhile; else: ?>
<?php _e(''); ?><?php endif; ?>
</div>

How can I make it repeat the cycle, start again at the first post, when all the posts have been cycled through?

At the moment it starts with the latest post and ends with latest. The first post has content of all other posts and no link for next post. The second post and all the following (except the last), have the right content, feature image and nicely link to the next post.

Thanks.

S

1 Answer 1

0

If I understood you right you want to list all of your posts on one page. To do so, try something like this:

 <?php
  if ( have_posts() ) {
      while ( have_posts() ) {
          the_post();
      ?>
      <a href="<?php next_post_link(); ?>">
      <div class="featured-image"><?php the_post_thumbnail(); ?></div>
      <div class="content"><?php the_content(); ?></div>
      <?php
      } // end while
  } // end if
 ?>
7
  • That's what I thought as well but the answer seemed too easy. :-)
    – cowgill
    Commented Oct 27, 2016 at 10:05
  • Thanks Sebastian, but no help! What I need is one post (feature image+content text) per page. When you click on feature image for example, then next post loads per page. When the last post (by adding date) appears, clicking on feature image loads the first post again. One post per page. Infinite cycle of posts by adding date, starting with the latest post added. Hopefully that explains my needs! S
    – sizer
    Commented Oct 27, 2016 at 11:15
  • I see what you want to achieve. Try doing this: In your main php file (I guess it's index.php) do something similar to this: <?php $query = new WP_Query( array( 'posts_per_page' => '1', 'order' => 'DESC', 'orderby' => 'date' ) ); while($query->have_posts()){ $query->the_post(); } ?> <div><?php the_post_thumbnail(); ?></div> <div class="content"><?php the_content(); ?></div> next_post_link(); <?php } ?> Then in your content.php file write this: <?php the_content(); next_post_link(); ?> I hope you know what I mean. It's quite complicated but it works for me Commented Oct 27, 2016 at 12:52
  • Thanks again Sebastian! I wrote all the code to index.php, this is how it looks. <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 the_content(); ?> </div>
    – sizer
    Commented Oct 27, 2016 at 14:21
  • Two problems. 1. The content text is always the same on every post. 2. The cycling is not complete. It will not link back to the latest post, to create an endless post cycle. S Edit: I used <?php echo get_permalink( get_adjacent_post( false, '', true ) ); ?> for next post link url.
    – sizer
    Commented Oct 27, 2016 at 14:23

Your Answer

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

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