10 Little Known WordPress Theme Tricks

Published September 16, 2008 19 Comments 0 Delicious Bookmarks

WordPress will never cease to amaze me. I have been working with WordPress for years now, designing and programming projects like Pittsburgh Designers, adClustr and this site — and I am always learning new ways to improve the WordPress experience. This is my list of the hidden theme tricks I’ve amassed over the years.

1. Multiple & Dynamic Sidebar Templates

Does trying to remember the TEMPLATEPATH syntax get to you sometimes? Do you think this is way too complicated?

<?php include (TEMPLATEPATH . '/sidebar-sample.php'); ?>

Ok, maybe not, but now starting in WordPress 2.5, we can use this code to include the same exact template file (sidebar-sample.php):

<?php get_sidebar('sample'); ?>

Now that we know this, it is easy to create a much more streamlined sidebar based on categories…

<?php
    //to be able to use this outside the_Loop
    if ( have_posts() ) { the_post(); rewind_posts(); } 

    if ( in_category('1') ) {
        get_sidebar('cat1');
        //gets sidebar-cat1.php
    } elseif ( in_category('2') ) {
        get_sidebar('cat2');
        //gets sidebar-cat2.php
    } elseif ( in_category('3') ) {
        get_sidebar('cat3');
        //gets sidebar-cat3.php
    } elseif ( in_category('4') || in_category('5') || in_category('6') ) {
        get_sidebar('catRest');
        //gets sidebar-catRest.php
    } else {
        get_sidebar()
        //gets sidebar.php
    }
?>

or by page type…

<?php
	//to be able to use this outside the_Loop
	if ( have_posts() ) { the_post(); rewind_posts(); }

	if ( is_home() || is_front_page() ) {
		get_sidebar('home');
		//gets sidebar-home.php
	} elseif ( is_archive() ) {
		get_sidebar('archive');
		//gets sidebar-archive.php
	} elseif ( is_page() ) {
		get_sidebar('page');
		//gets sidebar-page.php
	} elseif ( is_single() ) {
		get_sidebar('single');
		//gets sidebar-single.php
	} else {
		get_sidebar()
		//gets sidebar.php
	}
?>

2. If Comments are Allowed…

On a recent project, I had to find a way to display comments if they were enabled for the specific post, but otherwise display no evidence that comments ever existed on the page. I ended up wrapping the comments.php template call like this and it worked perfectly.

<!-- Added to show no evidence of comments when
the status has been changed to allow no comments -->
<?php if ('open' == $post->comment_status) : ?>
	<div class="commentsblock">
		<?php comments_template(); ?>
	</div>
<?php endif; // If Comments are Open Wrapper ?>

3. Category Slogans

Some blogs not only show what category a post belongs to, but also offer up a witty slogan to go along with it. You could always just name your category “The Amazing Open Source PHP Language ” instead of simply “PHP”, but how extensible is that?

What you can do however which is just as easy is to add the slogan as the description of the category. Then you can replace your get_categories() function call with

<a href="<?php echo get_category_link($cat);?>" title="View all <?php single_cat_title('prefix', 'display'); ?> Posts" ><?php echo category_description($cat); ?></a>

4. Styling the Current Category

Markup and styling of category lists is easy now that WordPress automatically inserts a class=”current” on the category used in your post in your archives list when using the template tag wp_list_categories(). It doesn’t get any easier than that – all we need to do is add some CSS to your stylesheet, and this is working.

.current {
color:#cc0000;
font-weight:bold;
}

5. Return the Numbers of Comments, don’t Echo

I hate when functions assume that you want the data returned to you in the form of an echo. The template tag <?php comments_number('0','1 ','%') ?> automatically echos it’s data which means you can’t use it in any math computations or other functions as a variable. This is where we can use this undocumented function:

<?php get_comments_number(); ?>

Now you can do something slightly different with your comments count like use it to generate each post’s popularity score.

6. The Title without any Markup

This happened to me once. I was redesigning a site that used markup within the blog post’s title; “Little Known <strong>Wordpress</strong> Theme Tricks“. Knowing this template tag, my life would have been made much easier.

Instead of this:

<h3><a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title(); ?>"><?php the_title(); ?></a></h3>

…use this:

<h3><a href="<?php the_permalink(); ?>" title="Permalink to <?php the_title_attribute(); ?>"><?php the_title_attribute(); ?></a></h3>

This template tag provides a ‘clean’ version of the title by stripping HTML tags and converting certain characters (including quotes) to their character entity equivalent.

7. Different Layout for Page 2+

Here we can have a different template for any page that is not “page 1.” Place this code anywhere within the Loop. (Replace the comments with the code you want displayed on either page 1 or beyond)

<?php if ( !$paged < 2 ) {
  //display code for only page 2 or higher
} else {
  //display page 1 code
} ?>

8. Replace the_excerpt without a Plugin

Instead of using the the_excerpt template tag, the_content_rss. It allows for a little more flexibility, without the need of a plugin (no matter how great it is). This won’t do everything that gszub’s excerpt reloaded plugin does, but it will for someone that just wants a longer or shorter default excerpt.

<?php the_content_rss('', TRUE, '', 55); ?>

9. Built in Email Obfuscator

Hate when spam bots get a hold of your email? Use this template tag to help obfuscate it.

<?php echo antispambot(get_the_author_email()); ?>

10. Dedicated Options Page within the Dashboard

This is not a theme trick, but I found it very interesting none-the-less. This is a hidden admin page that displays the whole wp_options database table. Go to your dashboard and change the URL to match this:

../wp-admin/options.php

This entry was posted under Wordpress and has an RSS feed
19 Comments | Save to Del.ico.us |  

19 Comments

  1. jbj said:

    Great list! I didn’t knew some of your hacks and I’m glad I have found your site, thanks to WPVote!

  2. Thanks! I have been to WPVote many times – great job with it. It’s a much needed resource for the WP community.

  3. pastcaring said:

    Great list.
    One question (bit of a newbie):

    2. If Comments are Allowed…

    When you say ‘wrap’, what do you actually mean? Where do I place this code in the comments.php file?

    Thanks

    pc

  4. Sorry for the confusion PC. There is a template tag that includes the comments.php file called <?php comments_template(); ?>. As you can see in the example above, I “wrapped” that template tag with additional PHP that checks to see if comments are allowed.

    In the end, I made no changes to the actual comments.php file. It’s just that the <?php comments_template(); ?> tag will never get called if comments are not “open’ for that particular post.

    Hopefully this makes a little more sense..??

  5. [...] I’m a WordPress fan myself, and to learn new tricks about WordPress is very exciting. Here is a very useful post of 10 little know WordPress theme tricks that you can use. View the post. [...]

  6. Shane said:

    Great list – I’ve learned some stuff :)

  7. Thanks guys! It took a while to collect these ones. At this rate, I’ll have another list ready in 6 months… haha

  8. Michael said:

    Nice list, thanks! But i have a little probleem with “if Comments are Allowed”.
    What is, if comments are disabled after maybe 5 months? You will never see the comments. The comments.php hides only the commentform.

  9. @Michael – Correct. I ran across this for a client that wanted to disable and remove previously approved comments (she was fed up with dealing with them) completely from her posts – but still wanted them in her admin panel to view as historical data. That’s what this code does – it doesn’t show any comment data if the post has comments turned “off” within the post-edit screen.

    BTW: Nice WP site you have there… keep up the good work!

  10. October 13th, 2008 @ 12:18 pm

    clark said:

    I see five articles a day with a title something just like this one. And usually they are so bad I can’t be bothered to even read them. That being said, congratulations. You’ve actually shown me some things I hadn’t previously seen anywhere. Great article.

  11. October 13th, 2008 @ 10:24 pm

    Thanks Clark! I love articles like this assuming that they are actually useful and not already published everywhere else. Hopefully i will have enough for a part 2 to this, but probably not anytime soon.

  12. November 4th, 2008 @ 2:50 am

    [...] goes to Chris Cagle for this awesome [...]

  13. January 14th, 2009 @ 12:07 am

    Tim said:

    Thank you for this article. The bit on get_comments_number() should really be in wordpress documentation. I’m used to Livejournal, where if there are 0 comments, there’s a link to respond to the post.

  14. Lillan said:

    Hi, where do i put the code <?php //to be able to use this outside the loop….and so on…to get a streamlined sidebar based on categories? is it in the sidebar or in main templatefile? It’s also calling for 4 categories, do i have to do 4 categorysidebars?

  15. March 18th, 2009 @ 3:47 pm

    [...] 10 Little Known WordPress Theme Tricks [...]

  16. April 8th, 2009 @ 7:00 am

    [...] This allows you to have an immense amount of sidebars, all different depending on which page of the site you decide to browse. The above code-snippet is courtesy of Chris Cagle. [...]

  17. May 19th, 2010 @ 10:54 pm

    I was trying to do a different sidebar for each category.

    This is normally very easy, selectively loading content by putting in the conditional if is_category directly in the sidebar.php.

    However, this does not work for the subsequent 3rd level post pages that appear below the main category page.

    For this, I needed to find the correct conditional statement to load a different sidebar based on in_category.

    After finding the code under #1 on this site:

    I was excited to have found a great solution, however, it was throwing an error in single.php, saying there was an extra “}”.

    After many hours of struggle, I finally debugged it, so it should read like this:

    the issue was the missing a trailing “;” after the final get_sidebar() statement.

    While I really appreciate the help with this code, I’m surprised no one else tried this out & had any problem with the syntax issues.

  18. July 15th, 2010 @ 10:19 pm

    Immanuel said:

    Thanks so much for tip 2 – worked a charm.

Leave your Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Get your own Gravatar