_s: Simplify image.php:

* Extracts logic in a custom template tag.
* Reduces switching in and out of php in `.entry-meta`.
* Minor code styling changes.

Fixes #233.
This commit is contained in:
obenland 2013-06-04 13:13:15 -07:00
parent 0e44f8479b
commit 6c83cfb5cc
2 changed files with 70 additions and 53 deletions

View file

@ -109,6 +109,55 @@ function _s_comment( $comment, $args, $depth ) {
}
endif; // ends check for _s_comment()
if ( ! function_exists( '_s_the_attached_image' ) ) :
/**
* Prints the attached image with a link to the next attached image.
*/
function _s_the_attached_image() {
$post = get_post();
$attachment_size = apply_filters( '_s_attachment_size', array( 1200, 1200 ) );
$next_attachment_url = wp_get_attachment_url();
/**
* Grab the IDs of all the image attachments in a gallery so we can get the URL
* of the next adjacent image in a gallery, or the first image (if we're
* looking at the last image in a gallery), or, in a gallery of one, just the
* link to that image file.
*/
$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'
) ) );
// If there is more than 1 attachment in a gallery...
if ( count( $attachments ) > 1 ) {
foreach ( $attachments as $k => $attachment ) {
if ( $attachment->ID == $post->ID )
break;
}
$k++;
// get the URL of the next image attachment...
if ( isset( $attachments[ $k ] ) )
$next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );
// or get the URL of the first image attachment.
else
$next_attachment_url = get_attachment_link( $attachments[0]->ID );
}
printf( '<a href="%1$s" title="%2$s" rel="attachment">%3$s</a>',
esc_url( $next_attachment_url ),
the_title_attribute( array( 'echo' => false ) ),
wp_get_attachment_image( $post->ID, $attachment_size )
);
}
endif;
if ( ! function_exists( '_s_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time and author.
@ -125,6 +174,7 @@ function _s_posted_on() {
);
}
endif;
/**
* Returns true if a blog has more than 1 category
*/
@ -158,4 +208,4 @@ function _s_category_transient_flusher() {
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', '_s_category_transient_flusher' );
add_action( 'save_post', '_s_category_transient_flusher' );
add_action( 'save_post', '_s_category_transient_flusher' );