Huge Compilation of WordPress Code

11:56:00 PM |


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 Recently Updated Posts/Pages

<?php
$today = current_time('mysql', 1);
$howMany = 5; //Number of posts you want to display
if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):
?>
<h2><?php _e("Recent Updates"); ?></h2>
<ul>
<?php
foreach ($recentposts as $post) {
if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
echo "<li><a href='".get_permalink($post->ID)."'>";
the_title();
echo '</a></li>';
}
?>
</ul>
<?php endif; ?>

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 Categories in Drop-Down Box

<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?>
<noscript><input type="submit" value="View" /></noscript>
</form>

Display Archives

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

Display Archives in a Drop-Down Box

<select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=\"\"><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>

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'); } ?>

Display an External RSS Feed

<?php include_once(ABSPATH.WPINC.'/rss.php');
wp_rss('http://wpforums.com/external.php?type=RSS2', 5); ?>

Display Most Recent Twitter Entry

<?php
// Your twitter username.
$username = "TwitterUsername";
// Prefix - some text you want displayed before your latest tweet.
// (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\")
$prefix = "";
// Suffix - some text you want display after your latest tweet. (Same rules as the prefix.)
$suffix = "";
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1";
function parse_feed($feed) {
$stepOne = explode("<content type=\"html\">", $feed);
$stepTwo = explode("</content>", $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = str_replace(”&lt;”, “<”, $tweet);
$tweet = str_replace(”&gt;”, “>”, $tweet);
return $tweet;
}
$twitterFeed = file_get_contents($feed);
echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
?>