If you are a theme designer, or just enjoy customizing your existing WordPress theme, it can sometimes be a hassle digging around for the WordPress code you need. I’ve spent a lot of time on this site collecting various snippets of code since launch and decided that it would probably be more convenient and useful to people if I was to consolidate them all into one post for easy reference.

Here are the snippets of code I’ve managed to collect. If you have any WordPress code you’d like me to add, please leave a comment below!

Display Recent Posts

Here is the code you need to display the most recent 5 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>

Display 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 10";
$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 Top Comments

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li>
<?php } } ?>

Display Categories

<h2>Categories</h2>
<ul>
<?php wp_list_cats('sort_column=name'); ?>
</ul>

Display Archives

<h2>Archives</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>

Display a Pages Menu in your Sidebar

<h2>Pages</h2>
<ul>
<?php wp_list_pages('title_li='); ?>
</ul>

Display Gravatars (WordPress 2.5+ Only)

<?php if(function_exists(’get_avatar’)){ echo get_avatar($comment, ‘50?);} ?>

Display Blogroll Links

<ul>
<?php get_links_list(); ?>
</ul>

Display Admin Section

<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="http://www.wordpress.org/">WordPress</a></li>
<?php wp_meta(); ?>
<li><a href="http://validator.w3.org/check?uri=referer">XHTML</a></li>
</ul>

Display a Pages SubMenu in your Sidebar

This will display any subpages in your blog’s sidebar:
<?php$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');if ($children) { ?><ul> <?php echo $children; ?>
</ul>
<?php } ?>

Display WordPress Tags

<?php the_tags(); ?>

Display WordPress Tags Cloud

<?php wp_tag_cloud('smallest=8&largest=36&'); ?>

Template Name

This allows you to use the WordPress page template to customize how a page is displayed:
<?php /* Template Name: Portfolio */ ?>

Dynamic Title Tags
<title><?phpif (is_home()) { echo bloginfo('name');
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo 'Category:'; wp_title('');
} elseif (is_search()) {
echo 'Search Results';
} elseif ( is_day() || is_month() || is_year() ) {
echo 'Archives:'; wp_title('');
} else {
echo wp_title('');
}
?></title>

Display PHP on a Single Page

Allows you to display plugins and such on a single page (replace home with the page you want it to only appear on):
<?php if ( is_home() ) { include ('file.php'); } ?>

I’m expecting this list to grow over time as I come across more useful WordPress code and update the post.

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |

There Are 18 Responses So Far. »

  1. 1 Rich
    Friday, November 16th, 2007 at 11:12 am

    Useful list there.
    Can I suggest an addition - the ‘comment-luv’ plugin over at
    http://www.fiddyp.co.uk/tag/commentluv/

    Cheers

    Rich

  2. 2 Website Design
    Thursday, November 22nd, 2007 at 2:35 pm

    It’s nice to see it broken down like this. Thanks. This will no doubt come in handy for quite a few people. Great post.

  3. 3 zeerebel
    Tuesday, December 11th, 2007 at 12:25 pm

    Very useful!!
    I was looking for something like this.

    Are there any other website with a running list of codes for wordpress ??

    Thanks,
    z

  4. 4 Criminal Background Check
    Tuesday, December 11th, 2007 at 3:31 pm

    Wow, look at that list. I am a former Blogger user, and being a bit more of a php developer myself I must say that I have found Word Press appealing simply because it uses php instead of some proprietary template format.

  5. 5 jessica elgin
    Tuesday, December 18th, 2007 at 11:05 am

    thanks! this is helpful.

  6. 6 Roman
    Wednesday, January 9th, 2008 at 3:37 am

    There we go, and I was thinking it was going to be harder than I imagined to edit my sidebars and add these types of lists.

    Thanks!

  7. 7 mondoreb
    Monday, March 17th, 2008 at 2:23 am

    hackwordpress:

    Can anyone help me? the WP forums seem to be not-so-good for essential help.

    I’ve downloaded and installed wordpress 4 times to my site. After importing my files from my blogger site, our new site experienced the following: 1-wiped out our ability to post; 2-lost our categories; and 3-lost our ability to manage posts.

    I’m not looking forward to a 5th install and download.
    Anyone know why this is happening? I’ve spent all weekend working on this, to no avail.

    thanks,

    mondo

  8. 8 F
    Sunday, April 6th, 2008 at 11:47 am

    whats the code for recent comments?

  9. 9 Mondoreb
    Sunday, April 6th, 2008 at 2:27 pm

    F:

    Well, I’m sorry. What do you mean by the code for the recent comments? it’s straight out of WordPress 2.3.3, whatever that is.

    Although I put in HaloScan a few days ago.

    But I’m not taking the chance of losing the whole thing again. It’s set us back about 3 weeks in getting our stories from our old blog over to the new site. We’re just cutting and pasting all the old stories. A slow process, to be sure, but at least it’s not going to screw things up.

  10. 10 Mondoreb
    Sunday, April 6th, 2008 at 2:27 pm

    F: thank you, by the way.

  11. 11 Laurence
    Thursday, April 17th, 2008 at 12:40 am

    Thanks mate, this page neatly summarizes all the things I have been scouring the WordPress documentation for.

    Cheers



Leave A Comment