[Resolved] Previous/Next Post But for Attachment Pages

Home Forums Support Previous/Next Post But for Attachment Pages

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1009057
    Vish

    Hi Tom

    I have a bunch of images in a post and when clicked they open the attachment page of the image. Now, I want to show the previous/next image (attachment) at the bottom similar to ‘Previous/Next Post’ functionality in SinglePosts. I have included a link which shows what exactly im trying to achieve.

    How can this be done in GP?

    #1009286
    David
    Staff
    Customer Support

    Hi there,

    you could use a function like this:

    
    <?php
    if ( is_attachment() ) {?>
        <div class-"attach-navigation">
            <span class="prev"><?php previous_image_link( false, 'Previous Image' );?></span>
            <span class="next"><?php next_image_link( false, 'Next Image' );?></span>
        </div>
    <?php
    }
    ?>

    And add it to a Hook element:

    https://docs.generatepress.com/article/hooks-element-overview/

    Use the after_content hook, check execute PHP and set the display rules to entire site.

    Then a little CSS to space out the links:

    .attach-navigation {
        display: flex;
        justify-content: space-between;
    }
    #1098526
    Vish

    David, thanks for the code.

    The code shows “Next Image” and “Previous Image”. Instead, I want to be able to show the actual image itself. Could you please help with that?

    #1098721
    David
    Staff
    Customer Support

    Try this instead:

    <?php
    if ( is_attachment() ) {?>
        <div class-"attach-navigation">
            <span class="prev"><?php previous_image_link( 'thumbnail' );?></span>
            <span class="next"><?php next_image_link( 'thumbnail' );?></span>
        </div>
    <?php
    }
    ?>
    #1108979
    Vish

    Exactly what I needed. Thanks, David!

    #1109600
    David
    Staff
    Customer Support
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.