WordPress.org

Ready to get started?Download WordPress

Forums

Next Image Links for Current Post Gallery Only (6 posts)

  1. lwynne
    Member
    Posted 1 year ago #

    For post image galleries, I need a code that uses some form of next/previous link to bring you to the appropriate next/previous image in that post and keeps the images in the correct order you chose while inserting them into the post.

    I've noticed every major news site (including those on WP) seems to have this feature. With all the WordPress updates, you would figure they would have done this by now! I'm not seeking a plugin, I'd rather have something I can add to functions.php

  2. bcworkz
    Member
    Posted 1 year ago #

    I know the twentyeleven theme gallery post format does this out of the box. I would think twentytwelve is similar. Regardless of which theme you use, if not already implemented, you can use the template tags previous_image_link() and next_image_link() on your theme's templates to implement this feature. The tags do not display anything if there is no next or previous image to go to.

  3. lwynne
    Member
    Posted 1 year ago #

    Using previous_image_link and next_image_link ends up providing links to images which were never inserted in the image gallery.

    Take a look here: only a penguin, lighthouse, and koala should show in the gallery.

  4. bcworkz
    Member
    Posted 1 year ago #

    The functions query for all attachment images who's parent ID is 2173. Somehow or another, those other images are attached to the post.

    I got a syntax error trying to use your gallery, you must be actively trying things on your template. Probably wouldn't have given me any extra insight anyway.

  5. lwynne
    Member
    Posted 1 year ago #

    I put a debug code in the gallery and I'm getting [post_parent] => 0 this suggests the post doesn't even realize which images are attached to the gallery! What could be causing this?

    The Post
    The Gallery

  6. bcworkz
    Member
    Posted 1 year ago #

    The Post link 404'd, I assume the gallery is normally launched from there? Oddly, the next and previous image functions do not get the parent from the attachment data. Instead, they call get_post() which makes a valiant effort to determine the current post, but it does not know about any attachment data either. There is nothing in your link to indicate what the current post is, which is why the parent ends up being 0.

    I'm not sure about the proper url parameters for gallery links, because I use permalinks. A gallery image permalink looks something like example.com/post-slug/attachment-slug/ so the parser always knows what the parent post is. It seems your gallery links are missing this critical bit of information.

    I'm guessing you need to either fix the gallery link construction or write your own previous and next functions that grab the parent from the attachment data.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags