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.
December 24th, 2010
at 4:01 am
These are a great list a of WordPress Snippets. It is hard to find good posts on things about wordpress coding.
January 22nd, 2011
at 5:39 am
Thanks very much for the code. I was looking for a way to add links to recent posts on a WP site where some of the theme files are hard-coded and my sidebar widget wasn’t working.
April 18th, 2011
at 3:42 pm
how to make the script text, and displaying the page posts
August 8th, 2011
at 10:50 pm
10 more Syntax Highlighters for WordPress http://www.fortystones.com/10-code-syntax-highlighter-wordpress-plugins/
September 22nd, 2011
at 6:27 pm
Amiin
September 22nd, 2011
at 6:32 pm
Boneka Full Body 0878 3191 8855
Harga: Rp 1,000,000.00
diciptakan untuk para pria yang main sendiri dikarenakan tidak memungkinkan untuk berhubungan intim (sex) dengan pasangannya Boneka Full Body
BONEKA made in Jepang ini terbuat dari bahan pilihan yang lembut, fleksibel dan berkualitas sehingga tidak menimbulkan penyakit dan iritasi pada kulit. UKURAN TINGGI : 165cm – BERAT : 4kg
BONEKA dapat di pompa dan di kemas, dalam vagina terdapat vibrator multi speed yang peng-operasiannya dengan remote sehingga dapat diatur frekuensinya sesuai kebutuhan.
- Badan boneka terbuat dari karet berkualitas tinggi, tidak mudah bocor & rusak.
- Vagina boneka berbahan silicon halus, tidak mudah rusak & sobek.
- Vibrator mengunakan batre AA X2.
- Vagina dapat dilepas, mudah dibersihkan.
- Vagina dapat bergetar, goyang, bersuara rintihan wanita.
BONEKA FULL BODY diciptakan untuk para pria yang main sendiri dikarenakan tidak memungkinkan untuk berhubungan intim (sex) dengan pasangannya. Dengan bentuk bagus dan bergengsi, produk ini dapat digunakan sebagai hadiah (kado) untuk teman atau relasi Anda.
PENTING !!! Hindarkan peminjaman produk untuk mencegah penyakit dan hal-hal yang tidak di inginkan..
Harga: Rp 1,000,000.00
January 29th, 2012
at 6:33 pm
thank you, I tried to design wordpress theme lately. And your article really helped me.
Thank you again,
February 14th, 2012
at 7:21 pm
salam kenal gan..!!
February 25th, 2012
at 12:27 am
allo malem sobat all
March 22nd, 2012
at 4:54 am
Wondering about expanding the recent posts to include up to 20 words of the body of the post? I went with your option 3.
March 22nd, 2012
at 4:57 am
http://news.vivaxmedical.com/ in the lower left box – I have two sample headlines. 3rd arrow should be displaying the headline from category 3 – yet it is not. I guess that’s a bigger issue than the abstract showing. ;)
March 22nd, 2012
at 4:29 pm
really helpful info..thanks for that
May 4th, 2013
at 7:34 pm
Today we pay our respects to the corking Nancy Cunard, heyday ahead Domain War I
– yet her occult-ish look continues to inspire account-minded manner insiders generations by and by.
http://www.quickmoneyking.co.uk/ As an illustration, the individuals lotion abrogating acclaim baronial wish online loanword covering
process is mere.
May 26th, 2013
at 10:58 pm
In That Respect are many do’s, don’ts time for card plowing!
best poker sites The .net framework presently propagate across major TV nets aerating poker presents
fifty-fifty as companions that go this form of military service that’s all for the fun of spieling poker.
May 27th, 2013
at 8:11 pm
I am composition want to fill in one lotion word
form including your personal inside information.
best uk payday loans For model, a mortal may penury money because he has issued fiery statements
in the terminal two weeks against…
June 16th, 2013
at 10:36 am
I quite like reading an article that can make men and women
think. Also, many thanks for allowing for me to comment!
June 16th, 2013
at 9:19 pm
Thank you for the good writeup. It in fact used to be a enjoyment
account it. Glance advanced to more added agreeable from you!
By the way, how can we communicate?
June 18th, 2013
at 8:36 am
I have read several just right stuff here.
Definitely price bookmarking for revisiting. I surprise how much attempt you put to
make such a magnificent informative site.