Opened 3 years ago

Last modified 2 years ago

#14408 assigned enhancement

Get author information in author template file without having to query the first post

Reported by: garyc40 Owned by: garyc40
Priority: normal Milestone: Future Release
Component: Themes Version: 3.0
Severity: normal Keywords: has-patch
Cc:

Description

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.

Attachments (2)

author-template-patch.diff (3.4 KB) - added by garyc40 3 years ago.
garyc40.14408.diff (1.7 KB) - added by garyc40 2 years ago.
set $authordata when is_author = true

Download all attachments as: .zip

Change History (6)

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to Future Release
  • Type changed from defect (bug) to enhancement

We can always just use the queried object.

  • Keywords needs-patch added; has-patch removed
  • Owner set to garyc40
  • Status changed from new to assigned

garyc402 years ago

set $authordata when is_author = true

  • Keywords has-patch added; needs-patch removed
Note: See TracTickets for help on using tickets.