Wordpress has something called loops in it's code. There are various loops in WordPress that can perform various functions such as display WordPress pages in a navigation bar automatically instead of hard coding it, displaying recent posts etc. but if your a serious blogger then you'll probably want to know the code that runs your blog. So here's some must have WordPress loops for you and your WordPress website.
Like I said there are so many loops in wordpress, small snippets of code that will do a big job and save you time. Loops have existed essentially since WordPress began, comparing loops that we're present in say 1.5 compared to the 2.x releases will quite possibily of changed now, But the whole greatness about WordPress is that it is reflexible and infact you'll find that there different loops to get the same result e.g. to display comments. Anyway here are some fantastic loops for a blog
Display recent posts:
If you don't directly list your pages on your index.php then no one will know which is the newest post straight away (Unless you point this out) so why not add in a little loop to display your latest posts:
<?php wp_get_archives('title_li=&amp;type=postbypost&amp;limit=5'); ?>
The value of &limit=5 is the amount of post you want to show up, a common number is 5, but you can change this to whatever suites you, if you wanted to do it in a latest post format you would simply set the value to 1. Which would display the latest post.
Display Navigation links:
If you have created pages within WordPress, then you'll most likely want them to show up automatically instead of hard-coding all in. To save you time there's a great loop to do this:
<?php wp_list_pages('title_li='); ?>
This loops works straight out of the box and requires no editing. However in order for the homepage to show up you may have to add a addition to the code. Because in most cases, the homepage of a blog site is not actually a page, it is the index.php of the theme being used. So if this is the case for you add this addition to the code:
<a href="<?php bloginfo('url'); ?>">Home</a>
<?php wp_list_pages('title_li='); ?>
Display Recent Comments (No Plugin):
Displaying comments besides at the end of any post can be useful, say if you wanted the recent five comments to be show somewhere, there is a loop that can do this, but it does require you to add some php to your functions.php page otherwise it won't work
The Loop:
<?php recent_comments(); ?>
Code to place within the function.php of your theme:
function recent_comments($num_comments = 5, $comment_lenth = 10, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 0) {
global $wpdb;
$request = "SELECT ID, comment_ID, comment_content, comment_author, comment_author_url, post_title FROM $wpdb->comments LEFT JOIN $wpdb->posts ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID WHERE post_status IN ('publish','static') ";
if(!$show_pass_post) $request .= "AND post_password ='' ";
$request .= "AND comment_approved = '1' ORDER BY comment_ID DESC LIMIT $num_comments";
$comments = $wpdb->get_results($request);
$output = '';
if ($comments) {
foreach ($comments as $comment) {
$comment_author = stripslashes($comment->comment_author);
if ($comment_author == "")
$comment_author = "anonymous";
$comment_content = strip_tags($comment->comment_content);
$comment_content = stripslashes($comment_content);
$words=split(" ",$comment_content);
$comment_excerpt = join(" ",array_slice($words,0,$comment_lenth));
$permalink = get_permalink($comment->ID)."#comment-".$comment->comment_ID;
if ($comment_style == 1) {
$post_title = stripslashes($comment->post_title);
$url = $comment->comment_author_url;
if (empty($url))
$output .= $before . $comment_author . ' on ' . $post_title . '.' . $after;
else
$output .= $before . "<a href='$url' rel='external'>$comment_author</a>" . ' on ' . $post_title . '.' . $after;
}
else {
$output .= $before . '<a href="' . $permalink . '" title="View the entire comment by ' . $comment_author.'">' . $comment_excerpt.'...<strong>' . $comment_author . '</strong></a> ' . $after;
}
}
$output = convert_smilies($output);
} else {
$output .= $before . "None found" . $after;
}
echo $output;
}
?>
You customize how the comments display via the code in the functions, specifically this part:
function recent_comments($num_comments = 5, $comment_length = 10, $before = '<li>', $after = '</li>', $show_pass_post = false, $comment_style = 0) {
The main two array's to define are num_comments and comment_length . To place this function code simply add it just before the closing tag of the php.
Pagination (Older and New entries links):
There are many ways to create pagination in wordpress, but whichever way you go about it your certainly going to need a link to older/newer posts, incast someone wants to scan the archives, so here's a simple out of box method:
<?php next_posts_link('Older Entries') ?></span><span class="next-entries"><?php previous_posts_link('Newer Entries') ?>
This loop needs to go in your index.php and after all of your post code
Admin Edit Link:
Say if your browsing your blog and you've noticed a spelling mistake on one of your posts, instead of going to wp-admin why not place a edit link on your blog posts and pages, so when your logged in, you can simply click edit and you'll automatically be directed to the edit page, saves you time by involving less clicks! Simply place this:
<?php edit_post_link(’Edit’, ”, ‘ | ‘); ?>
You can place this in either your single.php or index.php (For the edit link to appear on pages, you'll also have to add it to page.php too) This link only appears to a user thats logged in which also has the correct permissions, so only Administrators and editors will see the link, if the person does that have the needed permissions, the links does not display.
Displaying the amount of comments on a certain post:
For most blogs, the norm is that the latest five posts are displayed to you on the index page, well it would be nice for a reader to see how many comments that certain post has, here's a loop that can do just that:
<?php comments_popup_link(’No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?>
The loop will automatically check for the amount of comments on the post and return either no comments, 1 comment or 2 Comments (If it's more than 2 then it it will show that number obviously)
This does not display once you view the full post e.g. when you access the post from a link like this: http://myblog.com/2009/01/01/my-post
Display post tags:
When you create a post in WordPress you can add tags to it, tags are simply keywords that define that post and allow search engines to pull posts relating to a keyword searched because of the tag(s) set on it, some layouts do not display tags on posts or anywhere (my blog layout doesn't display them), simply because as long as you display the category of the post then they are not needed) but we're all different, so here's the loop to use:
<?php the_tags(’Tags: ‘, ‘, ‘, ‘<br />’); ?>
Place this in either the single.php or index.php
Displaying a post category
When you post something in wordpress you can file it in one or more categories, these are great as allow you to organise your posts relative to the content within the post, but I believe that a posts category should be displayed on the post and on the frontpage (If you use summary posts), use this loop to get the category or categories (Because you can have more than one set) displaying on a post:
<?php the_category(’, ‘) ?>
Place this within either the index.php or single.php
Posts and permalinks
When you post something, it will have a title, e.g. My blog post in a header tag. You'll notice on many blogs that the post title is hyperlinked to create this it involves using the permalink loop and a tiny bit of HTML, here's the loop:
<?php the_permalink() ?>
This loop will need to be defined as the href in a link like so for it to work:
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
Two new loops are also present in this code:
(When you hover you mouse of the hyperlink the post title is displayed)
(This is the loop to display the post title)
Easy way to display the date:
There are many scripts out there that can display the time, date etc in many ways, but in WordPress there is a loop that you can use to easily display a date of when a post was made:
<?php the_time(’F jS, Y’) ?>
Placing it either the single.php or index.php, and placing it directly under the post title, as a user can cleary see when the post was made (As it's good to know!)
Displaying the post author:
If you have a blog that has more than one person posting content, then this loop is defiantly useful as it allows you to display the author of the post, a scenario could be that John posted something two days ago, and now Mary has just posted something, but the author isn't displayed so there's no "Posted by "Author Name" but by adding in this loop you can easily achieve this:
<?php the_author() ?>
Place it in either your index.php or single.php
Well there's some basic but useful WordPress loops for you, enjoy!










Fire G
January 30th
James! You didn't give me the credit for the recent comments PHP!!
And, most of the (actually all but like 2) are NOT loops. They're template tags, or pre-defined functions if you want to say it that way.