Wordpress is a very powerful CMS (Content Management System). It has tons of capability and can be extremely powerful. There's only one thing in the way from all of this power and you as the blog owner - your theme. See, no matter how many awesome features such as threaded comments or breadcrumb navigation or even an "edit" link beside each post are added, you're limited by your theme that you are using. If you theme isn't "smart" enough to use these features, well, quite frankly, you can't use them.

This is a post for all the Wordpress theme developers out there. Here are only a few of the many great snippets of code that will take your current theme creation and make it that much better. Please, use them.

Tip 1- How to Disable Commenting on Posts Older Than 1 Month

A great way to reduce the amount of spam you receive is to disable the ability to comment on posts that are more than 1 month old. Just paste the following in your functions.php file. Note: To change the amount of time from 1 month, just replace 30 (days) with any number of days you want.

<?php
function close_comments( $posts ) {
	if ( !is_single() ) { return $posts; }
	if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
		$posts[0]->comment_status = 'closed';
		$posts[0]->ping_status    = 'closed';
	}
	return $posts;
}
add_filter( 'the_posts', 'close_comments' );
?>

Tip 2 - How to Add an Automatically Changing Copyright Year in Your Footer

This is a common sight on most sites, something like a "Copyright 2007 - 2009" or something similar in the footer. Thing is, it's been more than a month since the new year and yet I'm still seeing "2008" as the current year on some sites. This is how you can make Wordpress change the date for you. Put in this snippet of code wherever you want the dynamic date to be displayed.

Copyright &copy; 200x-<?php echo date('Y'); ?> Example.com.

Tip 3 - How to Display a List of Allowed HTML Tags for Use in Comments

Ever been to a blog where you've seen a list of all of the allowed HTML tags right above the comment form? Ever wonder how to do that in Wordpress? Well, wonder no more.

You may use: <?php echo allowed_tags(); ?>.

Tip 4 - How to Add an "Edit" Link Next to Each Post

Ever see an error in one of your posts and sigh at the fact that you have to now go an navigate through the Wordpress Dashboard just to get to the post to edit it? Not anymore. Just add this next in single.php, index.php, or wherever a post appears. Note - Only admins can see the edit link.

<?php edit_post_link('Edit', ''); ?>

Tip 5 - How to Remove Curly Quotes From Your Posts

Ever copy and paste some php code you found on a website and find out that all of the quotation and apostrophe marks were causing errors? This is because of the way Wordpress styles these punctuation marks to make them more aesthetically pleasing. Who needs that, right? Just paste this code into your functions.php file and you're good to go.

<?php remove_filter('the_content', 'wptexturize'); ?>

Tip 6 - How to Remove Curly Quotes From Your Comments

Same thing as above, only this time, the code removes the curly quotes from your comments instead of posts.

<?php remove_filter('comment_text', 'wptexturize'); ?>

Tip 7 - How to Disable Search Engine Indexing on a Certain Category

This code snippet is more for SEO purposes, possibly to avoid duplicate content or something similar. Make sure search engines don't index any posts in a certain category by applying this to your head tags in header.php. Note - Change the category number of 4 to the category you want to prevent search engines from seeing.

<?php if ( is_category('4') || in_category('4') ) {
    echo '<meta name="robots" content="noindex">';
}

Tip 8 - How to Display the Total Number of Posts on Your Blog

A useful code snippet that displays how many posts you've made.

<?php $numposts = $wpdb->get_var("SELECT count(*) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post'");
if (0 < $numposts)
     $numposts = number_format($numposts);
echo $numposts.' posts.';
?>

Credit to Shawn for fixing this code for me.

Tip 9 - How to Add a Simple "Tweet This" Link to Each Post

Twitter is getting more and more popular each day. To make this benefit you, why not add a nice little "Tweet This" button to each blog post? Put this somewhere in The Loop in single.php.

<a href="http://twitter.com/home?status=I just read <?php the_permalink(); ?>" title="Send this page to Twitter!" target="_blank">Tweet This!</a>

Tip 10 - How to Display Your Scheduled Posts

Scheduling your posts in Wordpress is a great feature. Create a sense of suspense for your readers by allowing them to see the title's of upcoming posts. Paste this anywhere in your theme files.

<?php
$my_query = new WP_Query('post_status=future&order=DESC&showposts=5');
if ($my_query->have_posts()) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php the_title(); ?>
    <?php endwhile;
}
?>

Tip 11 - How to Exclude Certain Categories From Being Shown

If, for whatever reason, you don't want certain categories to be displayed, paste this code into The Loop. Note - Replace "3" with the number corresponding to the category you want to block.

<?php
if ( have_posts() ) : query_posts($query_string .'&cat=-3'); while ( have_posts() ) : the_post();
?>

Tip 12 - How to Allow Styling of Individual Posts

This is a pain when themes don't have this built in. If the user of your theme wants to style a single post differently than all of the others, the only way for you to make this easy for them is by giving each post a unique identifier. To do this, simple make use of "the_ID" in The Loop.

<div class="post-container" id="post-<?php the_ID(); ?>">
    <!-- Post Content -->
</div>

Tip 13 - How to Add a Unique Identifier to Each Comment

This basically follows the same idea as above, only now it's applied to comments.

<div class="comment-container" id="comment-<?php comment_ID() ?>">
     <!-- Comment Content -->
</div>

Tip 14 - How to Separate Trackbacks / Pingbacks and Actual Comments

The comment area of your posts should be a place where your readers can talk and discuss things with you and eachother. It's annoying if this discussion is interrupted by a few trackback announcements. Tidy up the comment area by putting the comments in one pile and the trackbacks in another.

<?php if ( $comments ) : ?>
<?php foreach ($comments as $comment) : ?>
<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>
 
<!-- It's a comment -->
<!-- Comment content goes here -->
 
<?php } else { $trackback = true; }?> 
<?php endforeach; ?>
<?php if ($trackback == true) { ?>
 
<!-- It's a trackback -->
  <ol id="trackbacks-ol">
	  <?php foreach ($comments as $comment) : ?>
	  <?php $comment_type = get_comment_type(); ?>
	  <?php if($comment_type != 'comment') { ?>
	  <li>
		<?php comment_author_link() ?>
	</li>
	  <?php } ?>
	  <?php endforeach; ?>
  </ol>
 
<?php } ?>
<?php else : ?>
<?php endif; ?>

Tip 15 - How to Make Wordpress Use Pagination Instead of the Default "Next" and "Previous" Buttons

Pagenavi is one of the most popular Wordpress plugins. Instead of showing the default "Next Page" and "Previous Page" links, Pagenavi allows the visitors to select which page they want to jump to. This is how to build this function into your Wordpress theme.

1. The first thing you must do is download the plugin and extract the files.

2. Find the files called wp-pagenavi.php and wp-pagenavi.css and move them to your theme's directory.

3. Now you'll need to replace the old code that calls the default "Next" and "Previous" links with the new code that calls for Pagination. To do this, go to your theme files and replace this:

<?php next_posts_link('Previous entries') ?>
<?php previous_posts_link('Next entries') ?>

With this:

<?php
include('wp-pagenavi.php');
if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
?>

4. Now we have to do a tiny bit of editing in the wp-pagenavi.php file. So open it up and go to line 61 to find this:

function wp_pagenavi($before = '', $after = '') {
        global $wpdb, $wp_query;

One you found it, replace it with this:

function wp_pagenavi($before = '', $after = '') {
	global $wpdb, $wp_query;
        pagenavi_init(); //Calling the pagenavi_init() function

5. One last step. Make sure your blog can locate the pagenavi stylesheet by going into your header.php and pasting this between the head tags.

<link rel="stylesheet" href="<?php bloginfo('template_url');?>/pagenavi.css>" type="text/css" media="screen" />

Credit for this tip goes to CatsWhoCode.com. Thanks!

That's the end of that...

Well guys, I hope you learned something new today. I hope at least one of these Wordpress tricks should come in handy in the future. Good luck.

If you liked this post, stay updated. Follow me on Twitter or subscribe to our RSS Feed via email.

Related Posts

Like this post? Spread it!