StylizedWeb

Subscribe to our updates:

A Design, WordPress and Tutorials Blog.

Dedicated to helping you learn the art and science of the web.

Author description outside the WP main loop

This is a guest post from Lozbo who runs "Academic Lozbo" dedicated to multimedia articles, tutorials and projects.

Today, I present to you: a WordPress mini loop to output the author description info in any place among your template files (in the sidebar for instance). The author description is the biographical info you set in your WordPress user configuration menu. If you don’t know what WordPress is, this tutorial might not be for you, but you could start reading more about what a CMS is, and about Open Source CMS’s.

  1.  
  2. <?php query_posts('author=1&showposts=1'); // query for the loop ?>
  3. <?php while (have_posts()) : the_post(); // the loop itself ?>
  4. <?php if (is_author('admin')) { // another condition ?>
  5.  
  6. <li id="adminbio">
  7. <h2><?php the_author_posts_link(); ?></h2>
  8. <?php the_author_description(); ?></li>
  9. <?php } else { // what if there is no author? ?>
  10.  
  11. No info available
  12.  
  13. <?php } ?>
  14. <?php endwhile; //end wp loop ?>
  15.  
Detailed explanation

If you are unfamiliar with the WordPress Loop, I would suggest you read a bit more about it, though it is not necessary for the code to work, just if you want to understand how it works.

The comments in the above code are very illustrative, but if you haven’t understood completely here it goes.

  1.  
  2. <?php query_posts('author=1&showposts=1'); ?>
  3.  

This line tells WP to make a query to gather database info to display in a loop, in this case, I am limiting the query to get only posts from the author whose id is 1 (which is set for admin in a default WP installation), and to show only the latest 1 post (you could add more, if you wanted to use the loop for a different purpose, but for our intentions this will do). You can edit the author id to suit your context.

  1.  
  2. <?php while (have_posts()) : the_post(); ?>
  3.  

Normal WP beginning loop conditional cycle tags, or however it’s called.

  1.  
  2. <?php if (is_author('admin')) { ?>
  3.  

The previous line is an additional condition, I am not sure why if I remove it, the code does not work as expected, but it also allows you to display a no info message in case something goes wrong with the author (for example: not finding an author named admin). You can surely edit this to fit your needs.

  1.  
  2. <li id="adminbio">
  3. <h2><?php the_author_posts_link(); ?></h2>
  4. <?php the_author_description(); ?></li>
  5.  

Most of these are normal XHTML tags. I wrap everything inside a list item tag, to display it accordingly in my sidebar, but you might drop it if you don’t need it. I also added an id for CSS purposes. The PHP code inside the h2 tag displays a link to all the author posts, and the_author_description WP tag displays our precious author information (edit the output information in the WordPress admin backend menu, under each user settings).

You could also add a small variation of the author link (the code inside the h2 tag), like this:

  1.  
  2. <?php the_author(); ?>
  3.  

Basically, it displays a link to the author’s URL, also set in the user configurations. This URL might be, for example, a link to my main site if I am blogging in a sub domain, for example, in my own particular case: the URL I set in the WordPress installed in this sub domain (academic.lozbo.org) points back to just lozbo.org the root (of all evil). Remember that this info is available for you to edit in the WP administration panel, in the user options (top right menu, in a default latest WP version, 2.6 as of now).

  1.  
  2. <?php } else { ?>
  3. OMG! Admin does not seem to have info available!
  4. <?php } ?>
  5.  

These lines tell WP, in case no author named admin is found, it should display the previous paragraph. Edit accordingly.

  1.  
  2. <?php endwhile; ?>
  3.  

The WP loop finishes here. Depending on how many loops (or mini loops) you have, you might also need to set this at one point, for example, right after the previous code, add the following line:

  1.  
  2. <?php rewind_posts(); ?>
  3.  

As I say, it is not always necessary; if you only have the main loop and this, and this second loop is put in a file which comes after the first loop, it is not necessary (or so I understand, though I am not sure, please correct me if I’m wrong!).

Additional Notes

Anyway, I developed it with a one user (admin) author only self hosted installation of WP (lots of long words), in mind, though it should work in a multi user environment, as long as the author login name is admin and the user id is 1 (which are the default settings, but a lot of people likes to add a new user with another custom name, instead of admin, and then delete the admin user… well at least that is what I like doing anyway).

Also, I tested it in the latest version of WP (at the time of writing this post: 2.6), but it should work in all the 2.x branches I think, and as I am not familiar with WP legacy template tags and code (I never designed a complete template from scratch before) I don’t know if it works in earlier versions of WordPress, though if you are running an older version you should upgrade anyway. It’s really easy with SVN.

If you do not know what am I talking about here, probably this will work for you perfectly, just copy and paste it on your sidebar.php template file, or wherever you want to display it (in the footer perhaps?).

Leave a comment on Stylized Web Have some feedback? Leave a comment

15 Comments So Far

  1. Great article! =P

  2. Pingback: PHP Coding School » Blog Archive » php code [2008-09-27 13:22:56]

  3. Pingback: Author description outside the WP main loop : Wordpressadvice.com

  4. yes, very useful, thanks!

  5. thanks for this :-)

  6. By Dejan Cancarevic posted on September 27, 2008 at 5:26 pm

    you are welcome ;)

  7. Thanks for the tut. presumably we adapt use this method for other situations too right [like displaying post data] ?

  8. WOW WUT A GREAT SITE

  9. Pingback: WordPress post author description | StylizedWeb.com

  10. Pingback: SiberKültür

  11. Pingback: WordPress post author description | Castup

  12. The title of this post is deceiving – this method is actually NOT recommended to get author information outside The Loop. Please see this excerpt from the WordPress Codex documentation on query_posts:

    Important note
    The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should use get_posts() instead. Use of query_posts on Loops other than the main one can result in your main Loop becoming incorrect and possibly displaying things that you were not expecting.

    The query_posts function overrides and replaces the main query for the page. To save your sanity, do not use it for any other purpose.

  13. good good and good posts
    thank you admin
    ;)

  14. wow amazing i love it….perfect.

  15. great stuff :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>