Most Commonly Used WordPress Code Snippets

May24

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.

most-commonly-used-wordpress-code-snippets

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)
."&hellip;</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: , , ,

Comments

Wanna share your thoughts? Leave a message
42
  1. Thanks Dino, was looking for a couple of these.

  2. Allan
    May 29th, 2009
    at 6:47 am

    That’s very useful, I’ll be back to use these for my WordPress theme. Thanks

  3. robert
    May 29th, 2009
    at 1:56 pm

    wow, I’ll try those one. im newby in wordpress.. always a newby. thanks Dino

  4. dino
    May 29th, 2009
    at 10:49 pm

    @Allan and @robert, you’re welcome!

  5. Very usefull, thanks!

  6. 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.

    • dino
      June 3rd, 2009
      at 9:27 pm

      You’re welcome Bill. I’m glad that you find something on this site useful.

  7. JamesD
    June 11th, 2009
    at 2:54 pm

    Thanks for the useful info. It’s so interesting

  8. Cipher
    June 23rd, 2009
    at 4:01 am

    I want that syntax color scheme. Whats it called or what are the colors?

    • That syntax color scheme is called Vibrant Ink and it’s for Notepad++. You can download it free at Notepad++ website.

  9. Cipher
    June 23rd, 2009
    at 4:05 am

    I mean the header image.

  10. Hiro
    July 23rd, 2009
    at 9:05 am

    That’s a good articles. Some skills are useful for me!

  11. Richard
    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!

  12. Great code Snippets, thanks for sharing!

  13. Useful stuff… It would be great if you can post this as a coda clip

  14. Waow enjoyed reading your post. I submitted your rss to my google reader.

  15. 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…

  16. 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.

  17. Great code Snippets, thanks for sharing.

  18. These are a great list a of WordPress Snippets. It is hard to find good posts on things about wordpress coding.

  19. 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.

  20. how to make the script text, and displaying the page posts

  21. 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

  22. thank you, I tried to design wordpress theme lately. And your article really helped me.

    Thank you again,

  23. salam kenal gan..!!

  24. allo malem sobat all

  25. Wondering about expanding the recent posts to include up to 20 words of the body of the post? I went with your option 3.

  26. Hi just wanted to give you a quick heads up and let
    you know a few of the images aren’t loading correctly. I’m not sure why but I think its a linking issue.
    I’ve tried it in two different browsers and both show the same outcome.

  27. Nice list of code snippets, definitely worth while bookmarking for future use, I am planning on putting a couple of my code snippets up online.

    I think one of the most difficult things to achieve is a breadcrumb or nice pagination.

    Thanks for the snippets.

Find me on the Web

Other places you can find me

Daily Digest

Resource Sites for Inspiration

Extra Stuff

Represent!

Search this Website

Looking for something specific?