Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On many magazine sites, when you click through a gallery (which is done in WP with the image.php template) the last click brings you to a "More Galleries" page. I would like to achieve something similar and would hope to do so by making a template for the "more galleries" page. The problem is I don't know how to make my next/previous links present a prev link on the last image which redirects to the "more galleries" page instead of another image.

I figure the best way to do this would be to add a statement to the previous link which sends the user to a predefined link (the more galleries page) when the previous link no longer links to an image... but I'm a little lost on how that would be done.

I'm also looking for other posssible workarounds for this if anybody has a better idea.

My current code:

<?php
        $parent = $post->post_parent;
        $attachments = array_values(get_children(array('post_parent' => $post->post_parent, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'ID') ) );
        foreach ( $attachments as $k => $attachment )
        if ( $attachment->ID == $post->ID )
          break;
        $next = $k+1;
        $prev = $k-1;
        $next_link = "";
        $prev_link = "";
        if ( isset($attachments[$next]) )
        {
          $next_exists = 1;
          $next_link = "<a href='" . get_attachment_link( $attachments[$next]->ID ) . "'>←</a>";
          $img_link = "<a href='" . get_attachment_link( $attachments[$next]->ID ) . "'>";
        }

        if ( isset($attachments[$prev]) )
        {
          $prev_exists = 1;
          $prev_link = "<a href='" . get_attachment_link( $attachments[$prev]->ID ) . "'>→</a>";
          if  (!$next_exists)
          {
            $img_link = "<a href='" . get_attachment_link( $attachments[$prev]->ID ) . "'>";
          }
        }
        if (!$next_exists and !$prev_exists)
        {
          $img_link = "<a href='" . get_attachment_link( $attachments[$k]->ID ) . "'>";
        }
        $img_url = wp_get_attachment_url( $attachments[$k]->ID );
        if (sizeof($matches) > 1)
        {
          $fn = "/home/phasenet/public_html" . $matches[1];
          $exif_data = read_exif_data($fn);
        }
      ?>

How I currently call the links:

<?php echo $prev_link ?>
<?php echo $next_link ?>
share|improve this question

1 Answer 1

up vote 0 down vote accepted
+50

Put this into your functions.php. You have to replace the link path in $gallery_page with the link to your more galleries page.

<?php
/**
 * Display previous image link that has the same post parent.
 *
 * @since 2.5.0
 * Original version in /wp-include/media.php
 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
 * @param string $text Optional, default is false. If included, link will reflect $text variable.
 * @return string HTML content.
 */
function lwy_previous_image_link($size = 'thumbnail', $text = false) {
    lwy_adjacent_image_link(true, $size, $text);
}

/**
 * Display next image link that has the same post parent.
 *
 * @since 2.5.0
 * Original version in /wp-include/media.php
 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
 * @param string $text Optional, default is false. If included, link will reflect $text variable.
 * @return string HTML content.
 */
function lwy_next_image_link($size = 'thumbnail', $text = false) {
    lwy_adjacent_image_link(false, $size, $text);
}

/**
 * Display next or previous image link that has the same post parent.
 *
 * Retrieves the current attachment object from the $post global.
 *
 * @since 2.5.0
 * Original version in /wp-include/media.php
 *
 * @param bool $prev Optional. Default is true to display previous link, false for next.
 */
function lwy_adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
    global $post;
    $post = get_post($post);
    $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') ));

    foreach ( $attachments as $k => $attachment )
        if ( $attachment->ID == $post->ID )
            break;

    $k = $prev ? $k - 1 : $k + 1;

    if ( isset($attachments[$k]) ) {
        $nav_link = wp_get_attachment_link($attachments[$k]->ID, $size, true, false, $text);
    }
    // This if statement has been added
    if (strpos( $nav_link, '<a ') === false ) {
        $text = $text ? esc_attr($text) : ''; 
        $title = __('More galleries', 'lwy_translate');    // text which is displayed instead of Next/Previous 
        $gallery_page = '/link_to/more/galleries_page';    // replace with URL to the more galleries page
        printf('<a href="%1$s" title="%2$s">%3$s</a>', $gallery_page, $title, $text);
    }
}
?>

If you use theme Twenty Eleven the previous/next links in template file image.php should look like

<span class="nav-previous"><?php lwy_previous_image_link( false, __( '&larr; Previous' , 'twentyeleven' ) ); ?></span>
<span class="nav-next"><?php lwy_next_image_link( false, __( 'Next &rarr;' , 'twentyeleven' ) ); ?></span>

If you do not use theme Twenty Eleven, replace 'twentyeleven' with the chapter name of your theme/subtheme translation files. As the text "More galleries" probably does not exist in any theme translation files, you have to create one by yourself to support it's translation. If not, the english version will be displayed in all languages.

As this code uses partially code from the WP source it should be reviewed for changes with new versions of WP.

EDITED 23.12.2011 This is an improved version of the last part in function lwy_adjacent_image_link(). Don't forget to replace the link to the more galleries page with the actual link.

<?php
    if ( isset($attachments[$k]) ) {
        $nav_link = wp_get_attachment_link($attachments[$k]->ID, $size, true, false, $text);  // save in $nav_link instead of displaying with 'echo'
    }
    // This if statement has been added
    if (empty($nav_link) || $nav_link == __('Missing Attachment')) {
        // If a new version of WP has been released, check this code snippet to behave equivalent to function wp_get_attachment_link() 
        // in post-template.php. Also the string 'Missing Attachment' has to be checked for changes.

        // $text  = $text ? esc_attr($text) : '';                   // this is simply the text which you set when calling lwy_previous_image_link() / lwy_next_image_link()
        $text = __('More Galleries', 'lwy_translate');              // display a fixed text like 'More Galleries'
        $title = __('Go to more galleries page', 'lwy_translate');  // the text which does popup when mouseover the link
        $gallery_page = '/link_to/more/galleries_page';             // !!! REPLACE WITH THE URL TO THE MORE GALLERIES PAGE !!!

        $nav_link = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>', $gallery_page, $title, $text);
        $nav_link = apply_filters( 'wp_get_attachment_link', $nav_link, $attachments[$k]->ID, $size, true, false, $text );
    }
    echo $nav_link;
?>
share|improve this answer
    
Can you be a bit more specific on how to change the links if I'm not using TwentyEleven? I tried to remove it from the code and also tried to rename it, and it makes BOTH the next AND previous links go directly to the "more galleries" page defined... it ignores there are other images in the gallery. You can see this at: themeforward.com/demo2/?p=1791 –  AndrettiMilas Dec 22 '11 at 14:06
    
The first version is a bit buggy. It was much to late last time. I've added an improved and extended version. To use this replace echo $prev_link from your script with function lwy_previous_image_link( false, __( '&larr;' , 'twentyeleven' )). If no translation is needed you may use lwy_previous_image_link( false, '&larr;'). –  jot Dec 23 '11 at 19:50
    
I keep getting: "Warning: Cannot modify header information - headers already sent" –  AndrettiMilas Dec 23 '11 at 23:07
    
You get this message if already any character has been printed to the output and after this you try to modify the HTML output again. This may be due to a line break after a closing ?> in some files (not the template files). File functions.php should only contain one openening <?php at the beginning. Best is to avoid the closing ?> at the end. Don't copy the PHP tags enclosing my functions into functions.php. Only the functions itself. I've tested this on a quite clean installation and it runs like a charm. –  jot Dec 23 '11 at 23:22
    
Hopefully I'll get it to work... I've been doing it without the starting and closing php tags, but I still get the error. One last question - are you including the first two sections of code in the original answer for the links? –  AndrettiMilas Dec 23 '11 at 23:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.