7 WordPress Customizations

Just because your site is powered by WordPress, that doesn’t mean it should look like a blog! I’m going to show you a few ways to make your WP powered site look more like a static website.

Random Posts

If your site focuses on one category but uses more, it would be a good idea to have a random post from the main category in the sidebar. This isn’t hard to achieve at all, just throw this code in wherever you want it, change the category (Use the full name for the category, not the slug. It does accept spaces in the category name) and you’re away! You could even show a few random posts by altering the number after ’showposts’.

<?php query_posts(array('orderby' => 'rand', 'category_name' => Services, 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?>
	<?php the_title(); ?>
	<?php the_excerpt(); ?>
	<a href="<?php the_permalink(); ?>">Click here for more!</a>
 <?php endwhile; ?>
 <?php endif;?>

Latest Posts

Moving on slightly from the first example, you could show the latest post from one category. Similarly to the example above, you can alter how many posts are shown by changing the number after ’showposts’.

<?php query_posts('category_name=wordpress&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
	<?php the_title(); ?>
	<?php the_excerpt(); ?>
	<a href="<?php the_permalink(); ?>">Click here for more!</a>
<?php endwhile; ?>

Custom Default Gravatar

The plain grey default gravatar that comes packaged with WordPress is all well & good but wouldn’t it be nice to put your own image there? Well, slap this chuck of code into your functions./php file in your theme folder.

When making the image to go in there, keep in mind that it will need to be square. it can be any size but keep it small. 120×120px is a decent size, that gives you loads of room for styling decisions. You can always change it later.

Once you’ve made the image, upload it and change the path to it in the code, go into your WP dashboard / Settings / Discussion and down the bottom, your custom Gravatar should be there, just enable it and your done! Every comment that didn’t have a users Gravatar attached to it will now have the new default one.

if ( !function_exists('custom_gravatar') ) {
	function custom_gravatar( $avatar_defaults ) {
		$myavatar = get_bloginfo('template_directory') . '/folder_wordpress_is_installed_in/images/gravatar.gif';
		$avatar_defaults[$myavatar] = 'Custom Gravatar';
 
		return $avatar_defaults;
	}
 
	add_filter( 'avatar_defaults', 'custom_gravatar' );
}

Display All Posts

This one might be a little more specialized, but displaying all of your posts could be a good way of archiving posts especially if you don’t use the category system like a lot of personal blogs don’t. I use this on my personal blog actually.

<?php $first_post = 0; ?>
<?php while(have_posts()) : the_post(); ?>
	<?php
		$myposts = get_posts('numberposts=-1&offset=$first_post');
		foreach($myposts as $post) :
	?>			
 
	<p><?php the_time('j.m.y') ?> - <a href="<?php the_permalink(); ?>"><?php the_title(); ?>
 
	<?php endforeach; ?>
<?php endwhile; ?>

The $first_post variable alters the ‘offset’. That means if you want to exclude the first 2 posts, because the are featured somewhere else, than just throw the number ‘2′ in there, and the first 2 posts will be not be shown. leave it as ‘0′ to show every post.

Custom Length Excerpt

Being able to control the length of the excerpt on the fly opens up a whole world of design opportunities. The beauty of this code is that you can alter the length every time you write it! it also strips all HTML, so that means paragraphs are broken down and line breaks are taken away too. You can also choose how it ends, but that’s a global effect to this function.

function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."...";
echo $excerpt;
}
 
function content($num) {
$theContent = get_the_content();
$output = preg_replace('/<img[^>]+./','', $theContent);
$limit = $num+1;
$content = explode(' ', $output, $limit);
array_pop($content);
$content = implode(" ",$content)."...";
echo $content;
}

To call this function and get the custom length Gravatar, use this small chunk of code. Just alter the number to lengthen and shorten it accordingly.

<?php excerpt('20'); ?>

Use WordPress Conditional Statements

Being able to control what goes where is always a big plus on whatever you design and code. With WordPress these capabilities are not lost at all. In-fact, they’re made easier!

Lets say you use some jQuery on the comment form of posts, it would be useless and steal bandwidth if you included the jQuery library on every page. the better way to do it would be to only include the library on posts. This is the way you would achieve that.

<?php if(is_single()) { ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<?php } ?>

Don’t underestimate the power of these tags,. They can be used to load in different style-sheets, sidebars, footers etc. A complete list and usage examples are in the WordPress Codex.

No Dates In The URL!

Personally, nothing annoys me more (apart from IE6!) than seeing the month and year a post was posted. It has the date on there post, why the hell do I need it in the URL! In a world where short URL’s are ever more important, the date in the URL is just a waste of space. Having the category on the other hand, is acceptable, as it shows you what your about to look at.

If you just wanted to show the post name n the URL like most people do, go to your WP Dashboard / Settings / Permalinks and select ‘Custom’ then put /%postname%/ in the box.

Note that it’s best to do this when you start a WP site, otherwise people that have linked to your site when you had dates in the URL get sent to your 404 page, which isn’t good, no matter how funny your 404 page is.

That’s It!

Well, that’s it from me anyway. There are hundreds more ways to improve WordPress sites.

I hope I’ve given you an insight into what can really be done with WordPress. It is a powerful CMS and is only starting to be used for sites other than blogs. Maybe your site will be the next big thing, using some of these methods! Share with the rest of us if you have used any!

Don’t forget to subscribe via RSS or email and to follow me on Twitter!