By default, most WordPress themes display Recent Comments in some form or another. Unfortunately, it isn’t usually very user-friendly, so it will often get removed.

Until now, most of your other options required the use of a WordPress plugin, but today I ran across a great code snippet you can use to display recent comments in a useful format. I just tested it out on my test site and it looks good. Here is the code you will want to paste into your theme’s sidebar:

<h2>Recent Comments</h2>
<?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;?>

[via WordPress Garage]

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

Have you ever wondered how exactly you could choose which categories do and which don’t display your advertisements?   Maybe you have a Sponsored Review category and want to make sure that all posts filed in that category don’t display any ads.

This is something that you as a user of WordPress have the ability to do without to much effort.  What you need to do is paste the following code into your blog around your advertisement code:

<?php if ( !in_category(10) && !in_category(11) ) { ?>

This is your existing advertising code.

<?php } ?>

Where it says 10 and 11 you will want to place the number assigned to your categories that you want to censor advertisements from. You can repeat where it says ’&& !in_category(12)’ as needed until you’ve censored all categories.

Enjoy!

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

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! |

Have you ever noticed that many next generation web browsers will often detect a blog’s feed by displaying a feed icon on the right side of the address bar?  This is something many estabalished sites have taken the time to set up to encourage feed subscribers, but something that most WordPress themes don’t do by default.  

In order to instruct next generation web browsers to automatically detect your WordPress blog’s feed(s), you’ll simply need to do is make a quick modification to your header.php file and add the following code:

<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />

<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Comments Feed" href="<?php bloginfo('comments_rss2_url'); ?>" />

If your theme already has the first code in place for the RSS Feed, then you’ll just need to paste the second set of code in to display your RSS Comments Feed.  You can also swap out the PHP code and instead use your Feedburner feed address if you’d like.

If you aren’t sure if you should bother to do this or not, never underestimate the power of suggestion.  It is an unobtrusive way to promote your RSS Feed and Comments Feed while not using up any valuable screen real estate.

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

If you are looking for a way to generate more traffic and inbound links for your blog, and you have some web design talent, one of the best things you can do is contribute a few free WordPress themes to the WordPress community.

In order to focus on making your theme unique, I recommend starting out with a blank theme template. Jonathan Wold has created an excellent tutorial called How to Create Your Own WordPress Theme, in which he walks you through the basic setup of a generic WordPress theme. From there, you are then on your own to express your creativity and add some plugin integration for potential users of the theme.

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

About a week ago I was starting to work on collecting a bunch of WordPress PHP code that could be consolidated into one post for easy reference, when I ran across an incredible post that already does this. The author WPCandy calls the document a WordPress Help Sheet, and that is exactly what it is.

This document consolidates a large number of PHP code snippets into one PDF file that you can save on your hard drive for future reference, or you can print it out so its available at any time. This document seems like it would be exceptionally useful for theme authors, or anyone that likes to alter existing themes they have downloaded.

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