I’ve been developing WordPress themes for more than 2 years and based from my own experience and personal preferences, here are the most common WordPress code snippets that I use. Please do check it out and if you learn a thing or two, let me know in the comments.
Display Recent Posts
There are different methods I use for this kind of function and it depends on the need and how the template is laid out. You can check out the 3 methods that I use below. I hope you find it useful in your projects.
Method 1: This code displays the 10 latest posts. You can place this code anywhere on your template. For more info on wp_get_archives() you may want to check out the codex
<ul>
<?php wp_get_archives('type=postbypost&limit=10'); ?>
</ul>
Method 2: This is just basically the loop except that we defined a number of posts to be shown using the query_posts() function. You may want to read more on query_posts()
<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>
Method 3: This is I think the best way to display the recent posts since it’s very simple and you can customize the way the posts are displayed. You can use this code anywhere on your template. Read more about the get_posts() function.
<?php
$recentposts = get_posts('numberposts=12&category=4');
foreach ($recentposts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
Display Recent Comments
I found this piece of code somewhere around the web but I don’t know who the original source is. So the credit goes to him or her. This piece of code will display the 7 most recent comments.
<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 7";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "\n<ul>";
foreach ($comments as $comment) {
$output .= "\n<li>".strip_tags($comment->comment_author)
.": " . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\" title=\"on " .
$comment->post_title . "\">" . strip_tags($comment->com_excerpt)
."…</a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;
echo $output;?>
Display Categories
There are different ways in displaying categories and most of the time I like the categories to be displayed in a simple list.
<h4>Categories</h4>
<ul>
<?php wp_list_categories('use_desc_for_title=0&title_li=&show_count=0'); ?>
</ul>
Display Archives as Simple List
Method 1: This will display the archives in a month list. I usually place this on the sidebar. Learn more about wp_get_archives.
<h4>Archives</h4>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>
Method 2: This will display all the posts in a simple list. I usually use this on an archives page template.
<ul class="archives">
<?php
$myposts = get_posts('numberposts=-1&offset=0');
foreach($myposts as $post) :
?>
<li><small><?php the_time('d.m.y') ?></small> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
Display Tagclouds
This will display the tags in a tagcloud. This is the simplest on how to use it. For more details you may check out wp_tag_cloud() from the WordPress codex.
<?php wp_tag_cloud('smallest=8&largest=22'); ?>
Display Blogroll
This will display the links in a simple plain list. More on wp_list_bookmarks.
<ul>
<?php wp_list_bookmarks('title_li=&categorize=0'); ?>
</ul>
While I know that these are not all of the most commonly used WordPress snippets, I hope this post will still help out people who want to learn WordPress. I have written this post so it can serve as a reference for me and by all means you can make it your reference too. But if you want to learn more on WordPress functions you can always go to the Codex or you can get a copy of WordPress for Dummies.
Tags: Code Snippets, Codex, Snippets, WordPress

May 27th, 2009
at 7:29 am
Thanks Dino, was looking for a couple of these.
May 27th, 2009
at 11:39 am
You are welcome James.
May 29th, 2009
at 6:47 am
That’s very useful, I’ll be back to use these for my WordPress theme. Thanks
May 29th, 2009
at 1:56 pm
wow, I’ll try those one. im newby in wordpress.. always a newby. thanks Dino
May 29th, 2009
at 10:49 pm
@Allan and @robert, you’re welcome!
June 1st, 2009
at 8:16 am
Very usefull, thanks!
June 3rd, 2009
at 9:17 pm
I found your site through CSS Mania while looking for some inspiration for my portfolio redesign. Your site is the first one that had something (other than inspiration) that I can actually use. Thanks for these.
June 3rd, 2009
at 9:27 pm
You’re welcome Bill. I’m glad that you find something on this site useful.
June 11th, 2009
at 2:54 pm
Thanks for the useful info. It’s so interesting
June 23rd, 2009
at 4:01 am
I want that syntax color scheme. Whats it called or what are the colors?
August 10th, 2009
at 9:34 pm
That syntax color scheme is called Vibrant Ink and it’s for Notepad++. You can download it free at Notepad++ website.
June 23rd, 2009
at 4:05 am
I mean the header image.
July 23rd, 2009
at 9:05 am
That’s a good articles. Some skills are useful for me!
August 7th, 2009
at 5:20 am
I’m also interested in knowing what syntax color scheme you’re using for the code editor in your article image. Thanks!
August 10th, 2009
at 9:35 pm
That color scheme is called Vibrant Ink for Notepad++
August 12th, 2009
at 9:17 am
Great code Snippets, thanks for sharing!
October 15th, 2009
at 6:25 pm
Useful stuff… It would be great if you can post this as a coda clip
November 26th, 2009
at 2:04 am
Waow enjoyed reading your post. I submitted your rss to my google reader.
May 23rd, 2010
at 1:06 am
Thanks a lot! I am just learning Information.
And php and this was very easy to follow and helped a lot.
You really took time to explain every little bit.
Thanks again…
May 24th, 2010
at 10:30 pm
Thanks a lot! I am just learning Information.
Php and this was very easy to follow and helped a lot.
You really took time to explain every little bit.
Thanks again.
July 23rd, 2010
at 1:39 pm
Great code Snippets, thanks for sharing.