Better image navigation by eckert


if you are using the wordpress image.php to show your photos, you know that the next and previous photo option sucks.  I’m sure they’ll fix it in a soon-to-be-released update, but for now, use this:

Place this code in your functions.php -

add_action( ‘init’, ‘mf_text_based_image_links’ );

function mf_text_based_image_links() {

if ( function_exists( ‘mf_previous_image_link’ ) === false ) {
function mf_previous_image_link( $link_text=’Previous Image’ ) {
if ( $l = mf_adjacent_image_link( $link_text, true ) )
print $l;
else
print ‘<span class=”inactive”>’ . $link_text . ‘</span>’;
}
}

if ( function_exists( ‘mf_next_image_link’ ) === false ) {
function mf_next_image_link( $link_text=’Next Image’ ) {
if ( $l = mf_adjacent_image_link( $link_text, false ) )
print $l;
else
print ‘<span class=”inactive”>’ . $link_text . ‘</span>’;
}
}

if ( function_exists( ‘mf_adjacent_image_link’ ) === false ) {
function mf_adjacent_image_link( $link_text, $prev = true ) {
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]) )
return ‘<a href=”‘ . get_attachment_link( $attachments[$k]->ID ) . ‘”>’ . $link_text . ‘</a>’;
else
return false;
}
}
}

Then place this code in your image.php file where you want the navigation to appear -

<h2><small><?php mf_previous_image_link( ‘&laquo; Previous Photo’ ) ?></small> -
<a href=”<?php echo get_permalink($post->post_parent); ?>” rev=”attachment”><?php echo get_the_title($post->post_parent); ?></a> -
<small><?php mf_next_image_link( ‘Next Photo &raquo;’ ) ?></small></h2>

You can of course change the Previous Photo and Next Photo to whatever you prefer.

Tags: , , , , ,

Leave a Reply


It’s Called Web Design is proudly powered by WordPress
Entries (RSS) and Comments (RSS). Valid XHTML