My name is Ronald Huereca and am a new author here at WLTC. I come from a technical and business background with a degree in EET and a MS in Business Administration. I hope to share with the WTLC readers some of little intricacies of WordPress, whether it be theme or plugin related. I have written a few plugins, one called Feed Styler and the other Ajax Edit Comments. I currently write for my personal blog, the Reader Appreciation Project, and Devlounge. I am open to any and all feedback, so if there is something you’d like to see me write about, please let me know.
When coding a WordPress plugin, one thing that must be quickly mastered is that of the WordPress loop and global variables. Once mastered, a plugin author can tell which global variables are accessible, and which are not.
With certain WordPress Action and Filters, it’s easier to tell where within the loop certain code is executed. Sometimes, however, you don’t want to use an action or filter and want to simply have a template tag that is called from within the loop. When doing this, you want to make sure you know exactly what global variables you are trying to access and what result you’d like to achieve.
Within this post, I will dissect the WordPress loop so that you might have a better understanding of what variables are available to be called by template tags of functions called from within the WordPress loop.
The WordPress Loop
The WordPress loop is the loop that displays the posts on a particular page and also the comments if on a single post.
Within your default index.php theme file, the loop begins when you see this code:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
The two functions have_posts() and the_post() are both property functions that access methods within the WP_Query class.
The have_posts function checks to see if the current post number is less than the number of posts you choose to display in the Admin panel. The loop will then continue until the current post number reachers the maximum number of posts you have decided to show.
If you were to dissect the_post function, you would find that it searches for the global variable $post. The the_post function gets the first post (and subsequent posts through each loop iteration), and sets up the post data (author, multi-page, etc). This data is assigned to the $post variable globally, so you may access it as well.
After the_post() has been called, you have access to a multitude of template tags and global variables. Below are some examples of template tags that are now available:
- the_author: Retrieves the author’s name.
- the_title: Displays the title of the current post.
- the_content: Displays the contents of the post.
- the_permalink: Displays the URL for the post in the form of a permalink.
- the_ID:Displays the post ID.
Listed below are the global variable equivalents of each of those template tags:
- Global variable authordata can be used. You could call it in a function using this code:
global $authordata;
echo $authordata->display_name;The authordata global can also access: nickname, last_name, first_name, ID, user_email, user_url, user_login, description, and several others.
- Global variable post can be used. You could call it in a function using this code:
global $post;
echo $post->post_title;The post global can also access: ID, post_author, post_date, post_excerpt, comment_count, and several others.
- Global variable post can be used. You could call it in a function using this code:
global $post;
echo $post->post_content;Keep in mind that accessing the content this way is accessing the unfiltered and untouched post content. This is useful if you wanted to manipulate the content to your liking instead of WordPress having a say in the content appearance.
- There is no quick global access, but all the_permalink function does is echo out the results of the function get_permalink with the assumption that the $id variable is set.
- Global variable id can be used. You could call it in a function using this code:
global $id;
echo $id;
A Template Tag Example
Say you made a custom template tag called get_my_trackback that was to do something everytime a trackback was detected within the comments loop. You would place the function call within the foreach comments loop in the comments.php template file. This example assumes that you are on a single post using the single.php template file.
<?php foreach ($comments as $comment) : ?>
get_my_trackback();
<?php endforeach; /* end for each comment */ ?>
Within this get_my_trackback function, you can do something like:
function get_my_trackback() {
global $comment;
if (empty($comment)) { return; }
if ($comment->comment_type != 'comment') {
//do trackback stuff
}
}
The comment global allows you to access the comment database fields so you can do just about anything regarding the comment. Please notice that the global variable is not assumed to be set. You always want to make sure the variable has a value before proceeding so that errors are minimal.
Conclusion
Within this post the WordPress loop was dissected and global variables explained so that you may access them from a template tag in an include file or a WordPress plugin. If you have any questions, comments, or additions, please feel free to weigh in.
Congrats on becoming an author here Ron! Hopefully more people will be able to benefit from your WP knowledge 🙂
*EXCELLENT POST*! Thanks!
great article, I am looking forward to reading more from your side. also I like the fancy illustrations:)
Excellent post! Great info..
Just installed the Ajax Edit Comments WP Plugin actually – great upgrade.
Thanks,
–Steve
i can’t believe i manage to take some time to read it in full..:)
Great stuff man…yes..custom template tag is definately useful if you don’t want to clutter your theme code..for easy plugin intergration..all the plugin yo developed are superb…pingback and default comment separated also sure come handy..
there is one custom template tag i would like to see..something like a post feature..ye know let’s say i only want my latest post in category A to be in top of my frontpage…and if i post latest post in category B..it will not replace the first post from category A..:)
sure appreciate it if someone have similiar custom template tag without using plugin..thanks.
Thanks for all the feedback. Template tags do some in handy for organizing theme code.
Thanks heaps, this is a really useful tutorial.
Thanks for the tutorial. It has cleared up lot of confusion which I had over the LOOP. It is a great source for plugin authors like me. Thanks 🙂
Cheers,
Sudar
Thanks for the good explanation! It was usefull for me to get into WP loops.
Hi, this is all like reading a foreign language to me so forgive me if I’m completely in the wrong area. I’m looking for a way to get a list of all the blogs by a writer automatically listed on their individual bio page. Sort of a personal archive for each writer. Is this plugin loop something that would work for that?
Thanks
I think your subscribe to comments plugin is misbehaving…
I left a comment on the New Twist on Premium WordPress Themes post, and am now receiving new comment notifications on this and Printer Friendly Codex posts…
can you tell me what is the code for gettin a post category and parent category links like in the top of that post?
Very nice article.. I really wanted to know about all the global variables supported by wordpress, specially related to the comments section ^_^ Thanks for the share..
Thanks for the post – was struggling getting my head round how the loop worked but your diagrams helped a lot.