WordPress theming has become a important revenue source for many freelance developers and web designers, which is a pretty good reason to constantly improve your skills in that field. The following WordPress hacks teach you how to do some commonly wanted theming features.

1. Exclude a category from the homepage

Sometimes you will not want a category’s posts to appear on the homepage, for example if you want to use a category to display notes in your sidebar. In that case, this is the code you should in your loop (the bold line is the one excluding category number 7).

<?php while ( have_posts() ) {
the_post();
if (is_home()) if (in_category('7')) continue;
?>

2. Exclude a category from the RSS feed

For pretty much the same reason, you could want to prevent a category to be included in the RSS feed. For that you don’t even need to tweak your template files, you just need to append a query string to the feed URL, like in the following example.

https://www.designer-daily.com/feed?cat=-7

3. Displaying posts from one category in the sidebar

Well, since the reason I gave to exclude categories from the homepage is to display that category as sidenotes, I’ll show you how to do that part too. Of course you could use a plugin or a RSS widget (with the category’s feed) for that, but here is how I would do it.

<?php query_posts('cat=7&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?> <a href="<?php the_permalink(); ?>>
<?php the_title(); ?></a><br />
<?php endwhile;?>

4. Create a dynamic title tag

For obvious SEO purposes, you will want to create a title tag that changes according the the visited page. Here is the code I would add in the header.php file (or wherever your title tag is).

<title><?phpif (is_home()) { echo bloginfo('name');
} elseif (is_category()) { echo 'Category:'; wp_title('');
} elseif (is_search()) { echo 'Search Results';
} elseif ( is_day() || is_month() || is_year() ) {
echo 'Archives:'; wp_title('');
} elseif (is_404()) { echo '404 Not Found';

} else { echo wp_title('');
}
?></title>

5. Styling featured posts

sticky-post-wordpress

The ability of creating featured posts in WordPress has been made much easier with the 2.7 version, in fact a little “stick this post to the front page” has appeared in that version. All you need to do is to hit the checkbox and make sure that you have a class set for your post with this line.

<div <?php post_class() ?> id="post-<?php the_ID(); ?>">

Then you can use the .sticky class to  change the properties of the sticky posts, for example to make the text white on a black background you would put this in your stylesheet.

.sticky {color:#fff;background:#000;}

6. Exluding specific pages from a page list

You’ll have to know this one to create navigation menus, it’s actually quite simple to do. Just insert this bit of code.

<?php wp_list_pages('exclude=12' ); ?>

7. Create a page-specific template

This is also quite simple, first you have to create a new page template file, like in this picture.

page template example

On the very top of this template file, you must add this code.

<?php /* Template Name: Example page template */ ?>

Then make all the changes in the core of the template, add/remove sidebars or do whatever you want. When editing your page in WordPress admin you should see this option in the Attributes box.

wordpress-page-template

Please note: Sometimes it’s necessary to switch to the WordPress Default Theme, then back to your theme, for the Template choices to appear.

8. Highlight author comments

This is useful to make the author comments pop in the comment section, so the readers find the answers to their questions quicker. It can also avoid people pretending they are the author by commenting with his name. To style the author’s comments differently, you should open the comments.php template file and look for this piece of code (or something looking like it).

<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"></li>

Replace it by this one.

<li class="<?php if ($comment->user_id == 1) $oddcomment = "authorcomment"; echo $oddcomment; ?>"></li>

Then you can style it in your stylesheet with something like this.

.authorcomment { background-color: #555 !important; }

9. Display an external RSS feed

If you want to display a feed in the widget zone this is quite easy, you’ll just have to create a RSS widget. If it’s elsewhere in your template, you can use this short piece of code.

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

10. Add a print button to your blog’s articles

So you spent hours creating a specific stylesheet for people wanting to print your articles but nobody is printing them? Suggest them to print by adding a little print link or button, it’s easy, just insert this bit of code where you want the link to appear.

<a href="javascript:window.print()" rel="nofollow"><img src="../path/to/print-icon.png" alt="Printer icon" /> Print this!</a>

About the Author

author photo

Mirko Humbert

Mirko Humbert is the editor-in-chief and main author of Designer Daily and Typography Daily. He is also a graphic designer and the founder of WP Expert.