Handy WordPress Theme Hacks

WordPress themes are coming with more and more handy features, some you don’t even want.  As things evolve, clever and competitive designers are putting more of the handy features into themes and there becomes less of a need to hack one’s theme to expand functionality.

That being said, there are many theme hacks that are useful for almost all sites.  This is a compilation of handy hacks and tricks meant to streamline your site and add those tiny bits of better functionality.  These are all aimed at the novice to intermediate WordPress admin.  Only basic knowledge of coding and formatting is necessary for these.

But first, an important clarification: the loop.  hacks or plugin code often say something to the effect of “this tag must be within the loop”.  Anything placed in “the loop” will be repeated for each post displayed.  This is the loop:

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
// the code inside the loop
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

Display multiple loops

This can be helpful for customizing the presentation of the first post, or first several posts.  In order to do this, simply create multiple loops and limit which posts are shown in each.  To show just the latest post:

<?php query_posts('showposts=1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<div>
<?php the_content('Read more »'); ?>
</div>
Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?>
<?php endwhile; endif; ?>

The red code shows the key modification to the loop, limiting to just the latest post.

In order to display more posts, say the next 8, you would add a second loop:

<?php query_posts('showposts=9&offset=1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
Posted on <?php the_time('F jS, Y') ?> in <?php the_category(', '); ?>
<?php endwhile; endif; ?>

The offset parameter causes the theme to skip the first post and start with posts 2-9.  This would result in 9 posts displaying on the front page.  You would use the first loop to format the first post differently, or whatever your intention was.

Conditional Tags

Are my second favorite hack.  This lets you do virtually anything with a theme if the correct parameters are met.  This is handy in a few ways that I will describe.

The simplest version is the is_home conditional tag.  The following code will display markup if the page is the home page.  Note: this is different than is_front_page.  “Home” is the main blog page, and will only work if “latest posts” is the chosen front page display in the reading settings.  is_front_page includes both the blog page or a chosen static front page.  Make sense?

if ( is_home() ) {
   echo 'Will display this text only only the main blog page';
}

Some variations include:

is_single – for single posts and pages

is_sticky – needs no explanation

is_page – only for page content types

is_page( 5 ) – will display on page id 5 only

is_author( ‘4’ ) – will display if the author is id 4

There are many more applications, see them all in the official WordPress codex.

Custom Fields

My favorite handy WordPress theme hack, custom fields let you streamline entering custom data into posts, and customize how it is displayed via the theme.  The custom field parameters are set below the post content, but you can move them anywhere you’d like in the composition screen.

Start by creating a custom field name/key, and adding a value.  Here’s an example: I want the post title to link outside of my site.  This is the case for showcase or gallery sites that really only aggregate listings but don’t duplicate content with individual posts.  I would create a custom field called “url” and a value for each post would be the URL I want the title to link to.

Next, integrate your theme.  Change this:

<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>

to this:

<a href="<?php echo get_post_meta($post->ID, 'url', true); ?>">
<?php the_title(); ?>
</a>

This will change the post title to your custom field “url”.  Pretty nifty huh?

Use Body Class to display custom CSS

Designers often use filters to style a particular page.  For example, using a conditional statement to add a custom CSS class to an element like a menu, widget, etc.  Instead, and perhaps the better way to add custom styling to a specific element is to use a body class.  WordPress loads a particular CSS class depending on the page type, but this is optional.  If not specified, the standard body class will be loaded.  Using this code will specific the body class:

<body <?php body_class($class); ?>>

And here are some common body classes, courtesy of WPBeginner:

.rtl {}
.home {}
.blog {}
.archive {}
.date {}
.search {}
.paged {}
.attachment {}
.error404 {}
.single postid-(id) {}
.attachmentid-(id) {}
.attachment-(mime-type) {}
.author {}
.author-(user_nicename) {}
.category {}
.category-(slug) {}
.tag {}
.tag-(slug) {}
.page-parent {}
.page-child parent-pageid-(id) {}
.page-template page-template-(template file name) {}
.search-results {}
.search-no-results {}
.logged-in {}
.paged-(page number) {}
.single-paged-(page number) {}
.page-paged-(page number) {}
.category-paged-(page number) {}
.tag-paged-(page number) {}
.date-paged-(page number) {}
.author-paged-(page number) {}
.search-paged-(page number) {}

Were these handy tricks helpful?  Is there one I should have included?  Please share it in the comments below.

4 thoughts on “Handy WordPress Theme Hacks”

  1. If you’re going to republish old content, then maybe it’s an idea to at least update the code to get rid of deprecated functions.

    showposts=8 Really???

    showposts actually has been deprecated since around 5 years ago?

    and query_posts() is almost always doing_it_wrong(), much better is to use either WP_Query or – when adjusting the main query – pre_get_posts

  2. Hi Piet, Thanks for pointing out our gaffe. We have updated the post. I didn’t catch that deprecation. In fact, I still use the function in themes and should have updated my methods a while ago apparently.

    Thanks again,
    Cyrus

Leave a Reply

Your email address will not be published. Required fields are marked *