Support » Plugin: Media Library Assistant » Next/Previous image link on attachment page (using taxonomies)

  • Resolved Todd Garrison


    First of all, I LOVE this plugin. As a developer who builds websites for a living, the tool you created is a lifesaver. I have only begun to tap into its power, but I’m quickly learning all the wonderful things it can do.

    Currently I’m working on a photography website for a client. I use custom attachment taxonomies and terms so that my client can easily manage galleries by bulk uploading images and assigning them to the appropriate gallery via your plugin. It makes management SO much simpler. Images can also be associated with multiple terms and be included in different galleries without having to upload multiple versions of the image.

    An example of the shortcode I’d use for a particular gallery is as follows:

    [mla_gallery orderby="menu_order, ID ASC" size="thumbnail" post_parent=all tax_query="array(array('taxonomy' => 'image-gallery','field' => 'slug','terms' => 'desert-landscape'))"]

    All images on the site simply live in the media library and do not have parents (i.e. attached to a page). I rely exclusively on the custom taxonomy terms to manage and organize the site’s photos.

    The gallery thumbnails link to the attachment page, where we have additional (custom field) information, including print prices, sizing information, etc. I’m trying to use the next_image_link()and previous_image_link() functions to allow navigation to the adjacent photos in the gallery without requiring the user to go back to the gallery. This works fine, except for when a particular image belongs to more than one term, and the function’s results become unreliable.

    While I do have some basic PHP skills, solving this issue is above my pay grade. Is this an issue that can be resolved given my current situation, or is there a better way to approach this?

    Much thanks! -Todd

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author David Lingren



    Thanks for the good words, for your interest in the plugin and for this stimulating question.

    The function reference for next_image_link() starts with “This creates a link to the next image attached to the current post.” Since your images are unattached, I’m surprised it works at all. The next_image_link() and previous_image_link() functions both rely on another WordPress function, adjacent_image_link(), which does its own database query (including a post_parent restriction) to compile the list of attachments to work with. You’re getting some good results simply because NONE of your images are attached, so they ALL have post_parent=0. This is not a recipe for success, as you’ve discovered.

    WordPress 3.5 introduced the “ids=” parameter, of which the Gallery Shortcode Codex entry says:

    Specifying IDs in your shortcode allows you to include images in your gallery that aren’t necessarily “attached” to your post — that is to say, not uploaded from within your post or page. This flexibility allows you to create and embed any number of galleries containing any number of images!

    I suspect this, too, breaks the “image_link” functions but I haven’t tested that.

    Have you considered using a plugin such as the Photonic Gallery (supported by MLA), the Jetpack by (Carousel, mosaic, circles, squares, and a slideshow view) or JS Shadowbox? All of these support previous/next browsing.

    The next Media Library Assistant release will include a new way to separating the data selection and format/display parts of gallery production, so you can use [mla_gallery] to select the attachments you want and then hand off a list of their IDs to any other shortcode that accepts a list of IDs as input. If that’s of interest, it will be out in a week or so.

    Finally, I do see that the adjacent_image_link() function ends with a filter that could be used to replace the value it would otherwise return. If you’re tied to using these functions and your own code to control the previous/next functions I can look at somehow using this filter to use the ID list [mla_gallery] generates to return a more accurate result. Let me know if that would be worthwhile.

    Hi David, and thank you for the quick, thorough and thoughtful response. Let’s see:

    Ideally, I’d like for my client to be able to “create galleries” simply by checking the appropriate boxes for the taxonomy terms upon upload with the Media Library Assistant. It saves a lot of tedious work. It also allows him to check off multiple taxonomy term boxes via this method (hierarchical taxonomy).

    The [mla_gallery] shortcode does need to link to an attachment page and not an overlay or other lightbox-type effect, due primarily to the fact that there is additional information on the page (description, location, purchase information, sizing options, etc.)

    Before using the MLA Gallery, I simply created galleries via a taxonomy archive page, whereby each tax term displayed thumbnails of all the images in that term. The thumbnail linked to an attachment page (image.php), and this was fine, except navigation was an issue due the problems you already mentioned. I got around this to a certain extent by creating a new function (a modified adjacent_image_link) that used a tax query to pull the next/previous images in the same term (using menu_order to sort). It works great except for the fact that if an image belongs to more than one term, the image.php page doesn’t know which term to use, and results are again unreliable.

    I was hoping that the MLA Gallery plugin would somehow allow me to isolate images within a gallery, avoiding the “multiple terms” issue.

    I certainly DO NOT expect you to solve this problem for me, but I was curious if perhaps you might have a suggestion that would allow me to plod forward. I really love this plugin, and I think a breakthrough is somewhere close, but I need a nudge 😉

    Thank you again, David, for taking the time to reply to my earlier question.


    Plugin Author David Lingren



    Since you’re comfortable with some PHP coding, here’s one way to proceed. Create a shortcode version of your modified adjacent_image_link function, something like:

    [my_adjacent_image ids="1,2,3" my_current_id=2, my_direction=previous]

    You can use this shortcode as the display/format half of the gallery and you can use [mla_gallery] to generate the ids= list:

    [mla_gallery orderby="menu_order, ID ASC" size="thumbnail" post_parent=all tax_query="array(array('taxonomy' => 'image-gallery','field' => 'slug','terms' => 'desert-landscape'))" mla_alt_shortcode=my_adjacent_image my_current_id=2, my_direction=previous]

    By the way, in this particular example you could replace the tax_query with image-gallery=desert-landscape.

    Your shortcode can return whatever HTML your theme needs. If you want to code a general-purpose image.php page, you’ll need a way to pass the taxonomy and term information (maybe more) from the gallery page to the individual image page. There’s an earlier support topic that addresses this:


    Look down toward the bottom of that topic for the entry that begins “That’s excellent; we’re in the home stretch!” and read from there.

    If the above is incomplete or unclear, let me know and I’ll expand on it. I know we can find a solution that works for you.

    Thanks again for your interest and for your perseverance!

    Wow, amazing–thank you for the (again) detailed response.

    Your suggestion of passing parameters from the gallery page through to the attachment page are precisely what I need. I can then configure my modified function to stay within that term so the tax terms don’t get all jumbled up.

    I’m assuming I’d have to pass the same parameter on to the prev/next links as well, so that’s something I’ll try to figure out.

    I’m going to read through your suggestions again and see if I can get my bearings. I’m still learning PHP, so each step requires a little perseverance on my part, but I enjoy troubleshooting and making WP do new things.

    Basically, my goal is this:
    – Set up galleries based on taxonomy terms
    – link thumbnails to attachment page
    – provide prev/next navigation at the attachment page level, staying within the same term, although many images belong to multiple terms.

    If you think it’s possible, I’ll continue forging ahead. Thank you again so much for your patience and willingness to lend a hand!


    Plugin Author David Lingren


    I am positive your goal can be reached, and I’m glad you are enjoying the journey.

    I’m happy to work with you on issues related to your theme and image.php code, but it might be too site-specific for the MLA support forum. If you want to pursue this discussion offline, send me your e-mail address (or telephone number) and I will respond to continue the dialog. Go to the “Contact Us” page at our web site:

    Fair Trade Judaica/Contact Us

    Thanks for understanding.


    Agreed. Thank you once again for your considerable time put into helping me.


    Plugin Author David Lingren


    I have released MLA version 1.42, which includes pagination support for the [mla_gallery] shortcode. Complete information and examples are on the Settings/Media Library Assistant Documentation tab.

    I hope the new features will help with the pagination portion of your tasks. Let me know if you have any problems or further questions.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Next/Previous image link on attachment page (using taxonomies)’ is closed to new replies.