Attachment page navigation according to media tag
-
1/ I have a created page template (mediatag.php) to display thumbnails for tagged images. In this example let’s say we’re viewing 12 thumbnails of image tagged ‘People’.
2/ I click an image and go to the attachment page and it displays that single image – say it’s an image of a person in England at a Wedding – so this particular image has been given several tags – ‘People’, ‘England’, ‘Celebration’). This is working fine for me…
3/ Here’s the bit I require help with please. I want to have a next/prev link on the single attachment pages that navigates through the attachment pages for images that have also been given the same tag (the tag that the user originally came from in step 1/ – in this case it would be the 12 images tagged ‘People’).
So I guess I need a query that stores the term of the tag page I came from (the page in step 1/) and then on the attachment page somehow lets the next/prev links know that I only want to navigate through images that have been give that same tag. By default the next_image_link and prev_image_link just take you through the attachments in the order in which they were uploaded.
Here’s a short version of my mediatag.php
<div id="content-wrapper"> <ul class="intro-grid" data-columns> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php // get the attachments caption to use in the image's alt tag) $attachment_id = get_post_thumbnail_id($post_id); ?> <?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "thumbnail"); ?> <li class="item"><a href="<?php echo get_attachment_link($post->id); ?>" title="<?php the_title(); ?>" rel="attachment"><img src="<?php echo $att_image[0];?>" alt="<?php $attachment = get_post($attachment_id); echo($attachment->post_excerpt); ?>" /> <div> <h2> <?php $attachment = get_post($attachment_id); echo($attachment->post_excerpt); ?> </h2> </div> </a> <?php endif; ?> </li> <?php endwhile; ?> </ul> <ul style="float:left"> <li> <?php next_posts_link(__('Older photos', 'minimalism')); ?> </li> <li> <?php previous_posts_link(__('Newer photos', 'minimalism')); ?> </li> </ul> <?php endif; ?> </div>
And here’s the attachment.php
<div id="content-wrapper"> <div> <ul> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <?php $attachment_id = get_post_thumbnail_id($post_id); ?> <?php $terms = get_the_terms($post->ID, 'media-tags', ' ', ', ', ' ' ); echo '<ul>'; foreach ( $terms as $term ) { $term_link = get_term_link( $term ); if ( is_wp_error( $term_link ) ) { continue; } echo '<li><a href=\'' . esc_url( $term_link ) . '\'>' . $term->name . '</a></li>'; } echo '</ul>'; ?> <?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "thumbnail"); ?> <li class="item"><a href="<?php echo wp_get_attachment_url($post->id); ?>" rel="attachment"><img src="<?php echo $att_image[0];?>" alt="<?php $attachment = get_post($attachment_id); echo($attachment->post_excerpt); ?>" /> <div> <h2> <?php $attachment = get_post($attachment_id); echo($attachment->post_excerpt); ?> </h2> </div> </a> <?php endif; ?> </li> <nav> <span class="previous-image"> <?php previous_image_link( false, __( '← Previous' ) ); ?> </span> <span class="next-image"> <?php next_image_link( false, __( 'Next →' ) ); ?> </span> </nav> <?php endwhile; ?> </ul> <?php endif; ?> </div> </div>
I’m all ears! Thanks
- The topic ‘Attachment page navigation according to media tag’ is closed to new replies.