Forums

[resolved] previous_image_link() (13 posts)

  1. Kalon
    Member
    Posted 4 years ago #

    Hello,

    At the end of a post I use the following for the next/previous post navigation:

    <?php previous_post_link('<small>Previous post:</small>&laquo; %link') ?>
    
    <?php next_post_link('<small>Next post:</small>%link &raquo;') ?>

    This way, I don't get the "Next post:" or "Previous post:" text if there isn't a next or previous post.

    I would like to achieve the same thing with the next/previous image navigation in the gallery.

    At the moment I am using:

    <small>Previous image:</small><?php previous_image_link() ?>
    <small>Next image:</small><?php next_image_link() ?>

    This will still display the "next image" text regardless if there is a next image or not.

    What can I do with the next_image_link() and previous_image_link() so as to make it only display the next or previous image text if there actually is a next or previous image?

    Cheers,
    Kalon.

  2. Michael Fields
    Theme Wrangler
    Posted 4 years ago #

    Kalon,

    Hi. Unfortunately, these functions are not pluggable and don't return anything on failure. This will most likely be addressed in a future release. Until then, the best advice that I can offer you is to write your own functions (or use mine below) that do exactly as you want.

    Add To: functions.php

    function mf_previous_image_link( $text ) {
        $i = mf_adjacent_image_link( true );
    	if ( $i )
    		print $text . $i;
    }
    
    function mf_next_image_link( $text ) {
        $i = mf_adjacent_image_link( false );
    	if ( $i )
    		print $text . $i;
    }
    
    function mf_adjacent_image_link($prev = true) {
        global $post;
        $post = get_post($post);
        $attachments = array_values(get_children("post_parent=$post->post_parent&post_type=attachment&post_mime_type=image&orderby=\"menu_order ASC, ID ASC\""));
    
        foreach ( $attachments as $k => $attachment )
            if ( $attachment->ID == $post->ID )
                break;
    
        $k = $prev ? $k - 1 : $k + 1;
    
        if ( isset($attachments[$k]) )
            return wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
    	else
    		return false;
    }

    Add To: image.php

    <small><?php mf_previous_image_link( 'Previous Image' ) ?></small>
    <small><?php mf_next_image_link( 'Next Image' ) ?></small>

    Please let me know how this works!

  3. Kalon
    Member
    Posted 4 years ago #

    Thank you for your time mfields, your code works perfectly.

    My blog is currently on my localhost, while I develop it and find a host that isn't complete pants, but you can see the navigation part in the below screenshot:

    http://img177.imageshack.us/img177/4522/screen2xc1.gif

    Again, thanks for your help,
    Kalon.

  4. Michael Fields
    Theme Wrangler
    Posted 4 years ago #

    Kalon,
    Glad it worked for you!

  5. Firkraag85
    Member
    Posted 4 years ago #

    Hi,

    What about a custom image? For example arrows for previous and next.

    Thanks in advance!

  6. leinert
    Member
    Posted 3 years ago #

    Hi,
    I've been quit lucky after I read this thread, because such kind of function I am looking for. But after copy and past of that code sniplets I get this error message:

    Warning: array_values() [function.array-values]: The argument should be an array in /var/kunden/webs/leinert/wordpress/wp-includes/functions.php on line 1767

    Warning: Invalid argument supplied for foreach() in /var/kunden/webs/leinert/wordpress/wp-includes/functions.php on line 1769

    What has been changed in get_children?

    Please help! I do not want to wait for 2.6!

  7. Anonymous
    Unregistered
    Posted 3 years ago #

    get_children needs its arguments as an array now. This should be good for 2.6:

    function mf_previous_image_link( $text ) {
        $i = mf_adjacent_image_link( true );
    	if ( $i )
    		print $text . $i;
    }
    
    function mf_next_image_link( $text ) {
        $i = mf_adjacent_image_link( false );
    	if ( $i )
    		print $text . $i;
    }
    
    function mf_adjacent_image_link($prev = true) {
        global $post;
        $post = get_post($post);
        $attachments = array_values(get_children(
    Array('post_parent' => $post->post_parent,
          'post_type' => 'attachment',
          'post_mime_type' => 'image',
          'orderby' => 'menu_order ASC, ID ASC')));
    
        foreach ( $attachments as $k => $attachment )
            if ( $attachment->ID == $post->ID )
                break;
    
        $k = $prev ? $k - 1 : $k + 1;
    
        if ( isset($attachments[$k]) )
            return wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
    	else
    		return false;
  8. flammable
    Member
    Posted 3 years ago #

    Or, if you'd prefer, the author has changed it to a plugin:

    http://mfields.org/wordpress-plugins/text-based-image-links/

    :)

  9. pstagg
    Member
    Posted 3 years ago #

    Some may be interested in this variant:

    For image links I wanted something functional equivalent to:
    previous_post_link('<li>%link</li>');

    So thanks to this thread I came up with this:

    In functions.php:

    function ps_previous_image_link( $f ) {
        $i = ps_adjacent_image_link( true );
    	if ( $i ) {
    		echo str_replace("%link", $i, $f);
    	}
    }
    
    function ps_next_image_link( $f ) {
        $i = ps_adjacent_image_link( false );
    	if ( $i ) {
    		echo str_replace("%link", $i, $f);
    	}
    }
    
    function ps_adjacent_image_link($prev = true) {
        global $post;
        $post = get_post($post);
        $attachments = array_values(get_children(Array('post_parent' => $post->post_parent,
    	      'post_type' => 'attachment',
    	      'post_mime_type' => 'image',
    	      'orderby' => 'menu_order ASC, ID ASC')));
    
        foreach ( $attachments as $k => $attachment ) {
            if ( $attachment->ID == $post->ID ) {
                break;
    		}
    	}
    
        $k = $prev ? $k - 1 : $k + 1;
    
        if ( isset($attachments[$k]) ) {
            return wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
    	}
    	else {
    		return false;
    	}
    }

    In image.php:

    <ol>
    <?php ps_previous_image_link( '<li>%link</li>' ) ?>
    <?php ps_next_image_link( '<li>%link</li>' ) ?>
    </ol>

    Thanks guys.

  10. Tarmu
    Member
    Posted 3 years ago #

    @pstagg

    I only think that you have confused previous & next :D

  11. Tarmu
    Member
    Posted 3 years ago #

    @pstagg, can you help me, I would like to add #photo to the URL the generated thumbnail links to.

  12. Tarmu
    Member
    Posted 3 years ago #

    Or anybody else of course..

    I'm using the code above from pstagg. It's generating thumbnails to link to the next and prev images.

    I just want to add #photo to the generated url. Should be simple, but I can't find it.

  13. Tarmu
    Member
    Posted 3 years ago #

    no one?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.