_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

@ -15,7 +15,7 @@ get_header();
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
<div class="entry-meta">
<?php
@ -30,8 +30,9 @@ get_header();
esc_attr( strip_tags( get_the_title( $post->post_parent ) ) ),
get_the_title( $post->post_parent )
);
edit_post_link( __( 'Edit', '_s' ), '<span class="sep"> | </span> <span class="edit-link">', '</span>' );
?>
<?php edit_post_link( __( 'Edit', '_s' ), '<span class="sep"> | </span> <span class="edit-link">', '</span>' ); ?>
</div><!-- .entry-meta -->
<nav role="navigation" id="image-navigation" class="navigation-image">
@ -41,45 +42,9 @@ get_header();
</header><!-- .entry-header -->
<div class="entry-content">
<div class="entry-attachment">
<div class="attachment">
<?php
/**
* 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'
) ) );
foreach ( $attachments as $k => $attachment ) {
if ( $attachment->ID == $post->ID )
break;
}
$k++;
// If there is more than 1 attachment in a gallery
if ( count( $attachments ) > 1 ) {
if ( isset( $attachments[ $k ] ) )
// get the URL of the next image attachment
$next_attachment_url = get_attachment_link( $attachments[ $k ]->ID );
else
// or get the URL of the first image attachment
$next_attachment_url = get_attachment_link( $attachments[ 0 ]->ID );
} else {
// or, if there's only 1 image, get the URL of the image
$next_attachment_url = wp_get_attachment_url();
}
?>
<a href="<?php echo esc_url( $next_attachment_url ); ?>" title="<?php the_title_attribute(); ?>" rel="attachment"><?php
$attachment_size = apply_filters( '_s_attachment_size', array( 1200, 1200 ) ); // Filterable image size.
echo wp_get_attachment_image( get_the_ID(), $attachment_size );
?></a>
<?php _s_the_attached_image(); ?>
</div><!-- .attachment -->
<?php if ( has_excerpt() ) : ?>
@ -89,29 +54,31 @@ get_header();
<?php endif; ?>
</div><!-- .entry-attachment -->
<?php the_content(); ?>
<?php
the_content();
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', '_s' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php if ( comments_open() && pings_open() ) : // Comments and trackbacks open ?>
<?php printf( __( '<a class="comment-link" href="#respond" title="Post a comment">Post a comment</a> or leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', '_s' ), get_trackback_url() ); ?>
<?php elseif ( ! comments_open() && pings_open() ) : // Only trackbacks open ?>
<?php printf( __( 'Comments are closed, but you can leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', '_s' ), get_trackback_url() ); ?>
<?php elseif ( comments_open() && ! pings_open() ) : // Only comments open ?>
<?php _e( 'Trackbacks are closed, but you can <a class="comment-link" href="#respond" title="Post a comment">post a comment</a>.', '_s' ); ?>
<?php elseif ( ! comments_open() && ! pings_open() ) : // Comments and trackbacks closed ?>
<?php _e( 'Both comments and trackbacks are currently closed.', '_s' ); ?>
<?php endif; ?>
<?php edit_post_link( __( 'Edit', '_s' ), ' <span class="edit-link">', '</span>' ); ?>
<?php
if ( comments_open() && pings_open() ) : // Comments and trackbacks open
printf( __( '<a class="comment-link" href="#respond" title="Post a comment">Post a comment</a> or leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', '_s' ), get_trackback_url() );
elseif ( ! comments_open() && pings_open() ) : // Only trackbacks open
printf( __( 'Comments are closed, but you can leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', '_s' ), get_trackback_url() );
elseif ( comments_open() && ! pings_open() ) : // Only comments open
_e( 'Trackbacks are closed, but you can <a class="comment-link" href="#respond" title="Post a comment">post a comment</a>.', '_s' );
elseif ( ! comments_open() && ! pings_open() ) : // Comments and trackbacks closed
_e( 'Both comments and trackbacks are currently closed.', '_s' );
endif;
edit_post_link( __( 'Edit', '_s' ), ' <span class="edit-link">', '</span>' );
?>
</footer><!-- .entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->
</article><!-- #post-## -->
<?php
// If comments are open or we have at least one comment, load up the comment template

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' );