#14408 closed enhancement (fixed)
Get author information in author template file without having to query the first post
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 3.7 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Themes | Keywords: | has-patch |
Focuses: | 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 (5)
Change History (18)
#2
@
14 years ago
- Milestone changed from Awaiting Review to Future Release
- Type changed from defect (bug) to enhancement
#3
@
14 years ago
- Keywords needs-patch added; has-patch removed
- Owner set to garyc40
- Status changed from new to assigned
#5
follow-up:
↓ 6
@
12 years ago
- Milestone changed from Future Release to 3.7
Updated patch.
We still have to call the_post()
in author archives, it would be great if we could stop doing that. Maybe in 3.7?
#6
in reply to:
↑ 5
;
follow-up:
↓ 7
@
11 years ago
Replying to obenland:
We still have to call
the_post()
in author archives, it would be great if we could stop doing that. Maybe in 3.7?
Is that a separate patch/issue, or is that what is being fixed here?
#7
in reply to:
↑ 6
@
11 years ago
Replying to nacin:
Replying to obenland:
We still have to call
the_post()
in author archives, it would be great if we could stop doing that. Maybe in 3.7?
Is that a separate patch/issue, or is that what is being fixed here?
obenland clarified in IRC this is what is being fixed here.
Looking at the patch, I think it is in the wrong place. We don't set up any kind of globals within the loop — it only occurs in setup_postdata(). Also, it must only occur for the main query, not for any subsequent queries. In this case, calling setup_postdata() in WP_Query (even just for the main query) could lead to some side effects, as well.
I think the proper location for this is in wp::register_globals(), which already sets up $posts, $post, etc.
We can always just use the queried object.