_s: Simplify author template.

Set up author data for the author template immediately, rather than
waiting for the first the_post() call.
This removes the need to call the_post() and rewind_posts() in an
author template to print information about the author.

Fixes #346.
This commit is contained in:
obenland 2013-12-21 00:00:25 -08:00
parent 678e136006
commit a156f2cbab
2 changed files with 21 additions and 9 deletions

View File

@ -24,16 +24,7 @@ get_header(); ?>
single_tag_title(); single_tag_title();
elseif ( is_author() ) : elseif ( is_author() ) :
/* Queue the first post, that way we know
* what author we're dealing with (if that is the case).
*/
the_post();
printf( __( 'Author: %s', '_s' ), '<span class="vcard">' . get_the_author() . '</span>' ); printf( __( 'Author: %s', '_s' ), '<span class="vcard">' . get_the_author() . '</span>' );
/* Since we called the_post() above, we need to
* rewind the loop back to the beginning that way
* we can run the loop properly, in full.
*/
rewind_posts();
elseif ( is_day() ) : elseif ( is_day() ) :
printf( __( 'Day: %s', '_s' ), '<span>' . get_the_date() . '</span>' ); printf( __( 'Day: %s', '_s' ), '<span>' . get_the_date() . '</span>' );

View File

@ -66,3 +66,24 @@ function _s_wp_title( $title, $sep ) {
return $title; return $title;
} }
add_filter( 'wp_title', '_s_wp_title', 10, 2 ); add_filter( 'wp_title', '_s_wp_title', 10, 2 );
/**
* Sets the authordata global when viewing an author archive.
*
* This provides backwards compatibility with
* http://core.trac.wordpress.org/changeset/25574
*
* It removes the need to call the_post() and rewind_posts() in an author
* template to print information about the author.
*
* @global WP_Query $wp_query WordPress Query object.
* @return void
*/
function _s_setup_author() {
global $wp_query;
if ( $wp_query->is_author() && isset( $wp_query->post ) ) {
$GLOBALS['authordata'] = get_userdata( $wp_query->post->post_author );
}
}
add_action( 'wp', '_s_setup_author' );