Get author information in author template file without having to query the first post
|Reported by:||garyc40||Owned by:||nacin|
In the template file author.php, Twenty Ten queries the first post of the author to get the author's profile information. This introduces a bug when the author has no blog posts yet, then the global $authordata is not set, as a result, no author information is displayed. Also, having to rewind the query later in the template is counter-intuitive.
One may argue that there's no need to display author information if that author doesn't have any blog posts yet. But I disagree. Theme developers might want to list custom posts on the author template file as well. For some sites, the author page is served as a member profile page for subscribers. Therefore having to query the first post in order to get the requested author information is a flawed approach.
One better way to fetch author information in the author template is by getting the query var 'author' for the author ID. Then use get_author_meta() with the second parameter to get the desired information.
$author_id = get_query_var( 'author' ); $author_description = get_the_author_meta( 'description', $author_id );
I attached a patch that addresses this issue.
In this patch, get_author_meta() is also modified to handle the "display_name" field correctly by applying the filter "the_author" whenever this field is fetched.
Another approach, which I haven't tested yet, is to set the $authordata global variable whenever the author query var is set. If that's possible, then we no longer need to supply $author_id to get_the_author_meta(). If anyone is interested in testing this approach, go ahead and create a patch.
Change History (18)
- Milestone changed from Awaiting Review to Future Release
- Type changed from defect (bug) to enhancement
- Keywords needs-patch added; has-patch removed
- Owner set to garyc40
- Status changed from new to assigned
comment:11 nacin — 6 months ago
- Owner changed from garyc40 to nacin
- Status changed from assigned to accepted