Skip to content

Commit

Permalink
_s: Simplify author template.
Browse files Browse the repository at this point in the history
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 Automattic#346.
  • Loading branch information
obenland committed Dec 21, 2013
1 parent 678e136 commit a156f2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
9 changes: 0 additions & 9 deletions archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@
single_tag_title();

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>' );
/* 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() ) :
printf( __( 'Day: %s', '_s' ), '<span>' . get_the_date() . '</span>' );
Expand Down
21 changes: 21 additions & 0 deletions inc/extras.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,24 @@ function _s_wp_title( $title, $sep ) {
return $title;
}
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' );

0 comments on commit a156f2c

Please sign in to comment.