WordPress.org

Support

Support » Miscellaneous » Need help linking featured image in single.php to next post

Need help linking featured image in single.php to next post

  • 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?

    <?php
    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>
        </div>
        <!-- END post-thumbnail -->
    <?php } ?>

    Thanks, Alex

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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>
        </div>

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

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

    Thanks,
    Alex

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

    Much appreciated,
    Alex

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Need help linking featured image in single.php to next post’ is closed to new replies.
Skip to toolbar