Today, as the inaugural tutorial for this site, 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.
As I don’t like tutorials that do not show quick and fast the experienced users what they look for, I present without further ado, the glorious code (detailed explanations follow):
<?php query_posts('author=1&showposts=1'); // query for the loop ?>
<?php while (have_posts()) : the_post(); // the loop itself ?>
<?php if (is_author('admin')) { // another condition ?>
<li id="adminbio">
<h2><?php the_author_posts_link(); ?></h2>
<?php the_author_description(); ?></li>
<?php } else { // what if there is no author? ?>
No info available
<?php } ?>
<?php endwhile; //end wp loop ?>
Note on the previous code
As I was writing this tutorial, I remembered I can’t just copy and paste PHP (nor XHTML or other) code in WP posts just like that, so I installed Syntaxhighlighter, a WP plugin to render highlighted code in posts, but it looks like something is going wrong, outputting an additional unnecessary space in the opening php tag. I don’t understand why, I will be contacting the author of the plugin, but until I solve this I hope you can figure it out or just download/open this plain text file and copy the code from there.
Update! I installed another plugin, and now the code should be working just fine.
Update 2! The other plugin also sucked! But I got a better one which really seems to be working fine. It is called highlight.js.
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.
<?php query_posts('author=1&showposts=1'); ?>
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.
<?php while (have_posts()) : the_post(); ?>
Normal WP beginning loop conditional cycle tags, or however it’s called.
<?php if (is_author('admin')) { ?>
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.
<li id="adminbio">
<h2><?php the_author_posts_link(); ?></h2>
<?php the_author_description(); ?></li>
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:
<?php the_author(); ?>
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).
<?php } else { ?>
OMG! Admin does not seem to have info available!
<?php } ?>
These lines tell WP, in case no author named admin is found, it should display the previous paragraph. Edit accordingly.
<?php endwhile; ?>
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:
<?php rewind_posts(); ?>
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?).
Resources
I struggled a lot trying to figure this one out (I am not a programmer), and in my struggle I came across several good sites (some of them actually have a partial solution for what I wanted to do):
- Lorelle on wordpress
- Hack wordpress
- Wordpress Codex (I already knew the Codex, but didn’t know about this particular issue)
I even found this PHP cross reference website thing which also seems quite interesting, and helped me in my original attempt to display this info using the Planet Ozh WordPress Theme Toolkit functions file (I may talk about the Theme Toolkit in a following article or tutorial in this site), which worked partially with some functions I managed to put together…
The other way (not working! Try the code at the top!)
This other function I just mentioned, looked a bit like this (I don’t remember it exactly as it was as I dropped it after I figured out the aforementioned code and it doesn’t fully work as expected, though you can also learn a bit from it):
function adminbio() {
if (get_the_author_description()) {
print get_the_author_description();
} else {
print "The author does not say much about himself";
}
}
function adminlink() {
if (get_the_author_url()) {
print get_the_author_url();
} else {
print "index.php";
}
}
function admin_name() {
if (get_the_author()) {
print get_the_author();
} else {
print "Author";
}
}
This functions must be declared within a functions.php file on the root of your template directory, as I am using the Sandbox 1.6 as a base and Theme Toolkit, I already had more code on this file (actually I had to merge both files into one, which got me to worry a little bit but worked fine in the end), but I believe that if you create it from scratch it should work. And the code to be on the sidebar:
<?php if(isset($_GET['author_name'])) :
$curauth = get_userdatabylogin($author_name);
else : $curauth = get_userdata(intval($author));
endif;
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // Mini Loop to get the user ?>
<li id="adminbio">
<h2>About the author</h2>
<?php adminbio(); ?>
<?php endwhile; else: ?>
<?php _e('No posts by this author.'); ?>
<?php endif; ?>
Remember that this code might not produce the desired results. I think I didn’t do things properly, but it helped me to learn some things, if you do not find it interesting, just ignore it.
End notes
I labeled this tutorial as intermediate considering a general multimedia rating, because obviously a WordPress developer, coder or themer, would consider this as basic tutorial or even for total beginners. But my rating is based in a broader multimedia development context, not just CMS or code related.
Thank you for reading me, I hope you enjoyed it; if so: go and tell your friends about it! If not, only tell your imaginary friends.
Great opening tutorial. Since I can’t tell what this site’s focus is going to be, should I assume it will be stufffed with WordPress tutorials? Interesting.
If so, consider changing all those WPs to WordPress so you get the WordPress keyword search term catches, and make sure you call things by their proper names.
The mini-loop to accommodate blog author information is frequently requested, especially when it pulls information in from the WordPress Administration Panels for easy maintenance and configuration rather than manually adding information to the template page in the WordPress Theme. Good job!
Alright! Thank you very much for your comment and advice!
Where exactly yo mean to call things by their proper names? I thought I did it, but I this is my first theme so I don’t know where I made mistakes.
Thanks!
BTW: After writing this tutorial I learned there is an easier way to do this, really missed it on the beginning! So I will be updating this post to show this info.
In this site I will pour all kinds of multimedia development tutorials/articles/downloads, from Flash and XTHML/CSS to Audio/Video production to music/audio design to photography… all my hobbies!
I’m glad you clarified what you will be covering. It’s so important to get that message across to your visitors immediately.
As for the proper words, WordPress is spelled as WordPress, not Wordpress nor WP, as an example. You need to write with the keywords people use to search with in order to be found. I recommend you read Tips For Writing Good WordPress Tips which will help you write all the various tips you will be offering.
Congrats on the new site and good luck with your writing!
Alright! I really appreciate your feedback and time to read me! Yes, I must admit that this design is in beta, as soon as I finish I will also make it available as a download right here.
I will take a look at that site!
BTW: I use WP to look for WordPress related stuff on Google with positive results! I think I will have a wider chance if I take all sorts of spelling into account in this regard, or let me know what you think!
By the way, I find the link you provided useful but I do not agree with all that this person has to say. In fact, I strongly disagree on some of the points expressed there. I will be posting a rant about it later right here!
Update! I see you are the author of that post! Well it will be good to further discuss it! Let me gather my arguments and I’ll let you know about it!
Thanks for the info!
This topic is quite trendy in the net at the moment. What do you pay the most attention to while choosing what to write ?
Hi Random, I haven’t posted anything here for quite a while, but when I was doing it, the main reason I would choose something to blog about was what I was working on at the moment. I would also take a look at what was being said at the moment in other blogs I read… why is that?