Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

No need for the_post and rewind_posts in author archives. #36

Merged
merged 2 commits into from

2 participants

@kovshenin
Owner

We can get all the information we need about the author by using get_queried_object within is_author.

@kovshenin kovshenin No need for the_post and rewind_posts in author archives.
We can get all the information we need about the author by using get_queried_object within is_author.
dec5c61
@mfields

Related #53

@mfields

Nice! I like this a lot :)

One probablem with the code is that it is not compatible with WordPress 3.2. I did a little testing and it seems like the following works well in 3.2-trunk:

$author = get_queried_object();
if ( isset( $author->data ) )
    $author = $author->data;

Does anyone see issues with this approach?

One more thing to note: The return value of get_author_posts_url() is filterable in core, I think it is best to escape the value with esc_url() before passing to printf()'.

@azizur azizur referenced this pull request from a commit
@azizur azizur No need for the_post and rewind_posts in author archives.
Merge pull request #36 from kovshenin

Merge remote-tracking branch 'kovshenin/patch-5'
6e9cac0
@kovshenin kovshenin Correct use of get_queried_object() for author archives.
The ->data object (used previously) stores the author data, but the WP_User class has the __get magic method that grants access to that data, so using get_queried_object() without ->data is correct, and compatible with 3.2, which does not use magic methods. Also url-escapes get_author_posts_url.
103ae2c
@kovshenin
Owner

@mfields thanks for the insight! I did need a break back then and it's good that you didn't merge. I dug into core this morning and turns out the get_queried_object returns a WP_User class which has a __get magic method since 3.3 I believe, so you can access things like $author->ID and $author->display_name without accessing the ->data object, even if you don't see them when doing var_dump or print_r on the $author object. The data object is used to "hide" these attributes behind the __get magic method, so accessing it directly would actually be wrong.

I added a commit to the branch to address this, checked on 3.2 and against current trunk, also escaping the author's posts URL, good catch!

@mfields mfields merged commit 800b9eb into Automattic:master
@mfields

@kovshenin Nice work on researching this! And thanks for trimming down the code in archive.php :)

@kovshenin kovshenin referenced this pull request from a commit
@kovshenin kovshenin Revert "Merge pull request #36 from kovshenin/patch-5"
This reverts commit 800b9eb, reversing
changes made to ce4bef6.
461976d
@marksantiago marksantiago referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 14, 2012
  1. @kovshenin

    No need for the_post and rewind_posts in author archives.

    kovshenin authored
    We can get all the information we need about the author by using get_queried_object within is_author.
Commits on Aug 27, 2012
  1. @kovshenin

    Correct use of get_queried_object() for author archives.

    kovshenin authored
    The ->data object (used previously) stores the author data, but the WP_User class has the __get magic method that grants access to that data, so using get_queried_object() without ->data is correct, and compatible with 3.2, which does not use magic methods. Also url-escapes get_author_posts_url.
This page is out of date. Refresh to see the latest.
Showing with 3 additions and 10 deletions.
  1. +3 −10 archive.php
View
13 archive.php
@@ -25,16 +25,9 @@
printf( __( 'Tag Archives: %s', '_s' ), '<span>' . single_tag_title( '', false ) . '</span>' );
} 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 Archives: %s', '_s' ), '<span class="vcard"><a class="url fn n" href="' . get_author_posts_url( get_the_author_meta( "ID" ) ) . '" title="' . esc_attr( get_the_author() ) . '" rel="me">' . get_the_author() . '</a></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();
+ // Get the queried author data.
+ $author = get_queried_object();
+ printf( __( 'Author Archives: %s', '_s' ), '<span class="vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( $author->ID ) ) . '" title="' . esc_attr( $author->display_name ) . '" rel="me">' . esc_html( $author->display_name ) . '</a></span>' );
} elseif ( is_day() ) {
printf( __( 'Daily Archives: %s', '_s' ), '<span>' . get_the_date() . '</span>' );
Something went wrong with that request. Please try again.