Need help linking featured image in single.php to next post (5 posts)

  1. alexdelis
    Posted 3 years ago #

    I have a wordpress theme i'm customising. The theme has a masonry script which puts all the posts into an order and uses the featured image as the thumbnail. once you click a thumbnail it takes you to the single post page where the featured image is big.

    I'm stuck trying to change the url of the image in that page to go to the next post, every time I try it relinks to itself. Heres the current section from the single.php that I need help changing. Would anyone know how to change it correctly to do this?

    if (has_post_thumbnail() ) {
        //get full-sized image
        $full_img = wp_get_attachment_image_src(get_post_thumbnail_id(), 'full-size');
        <div class="post-thumbnail">
            <a href="<?php echo $full_img[0]; ?>" title="<?php the_title(); ?>" class="prettyphoto-link"><?php the_post_thumbnail('post-full'); ?></a>
        <!-- END post-thumbnail -->
    <?php } ?>

    Thanks, Alex

  2. you can use the get_next_post() function to return the post object of the next post.

    <?php $next_post = get_next_post();
    // Check to make sure a next post exists to prevent getting an PHP property of non object warning
    $url = is_object( $next_post ) ? get_permalink( $next_post->ID ) : '';
    // Get the title of the next post to use in the link title attribute
    $title = is_object( $next_post ) ? get_the_title( $next_post->ID ) : ''; ?>
      <div class="post-thumbnail">
            <a href="<?php echo esc_url( $url ) ?>" title="<?php echo esc_attr( $title ); ?>" class="prettyphoto-link"><?php the_post_thumbnail('post-full'); ?></a>
  3. alexdelis
    Posted 3 years ago #

    Hi Chris,
    Thanks i'll give it a go and let you know.

  4. alexdelis
    Posted 3 years ago #

    Thanks it works perfectly. Can i change it to go through the posts the other way by changing all the next's to prev?


  5. alexdelis
    Posted 3 years ago #

    Ah don't worry I got it to work.
    Thanks so much for the help and quick reply.

    Much appreciated,

Topic Closed

This topic has been closed to new replies.

About this Topic