WordPress has many ways to hack and tweak to get your desired result, some are simple functions within the loop, whilst some can be hacks right into the core.
I use WordPress frequently in personal and client projects so have built up a list of hacks I use in day-to-day WordPress development and today I’m going to share a few of my favourite and most used tricks.
Take Out The Trash
Since WP 2.9 you can alter the number of days posts stay in the trash, the default is 30 days, but if we add this to our functions.php page:
define('EMPTY_TRASH_DAYS', 7 );
We can specify the amount of days, in this example it’s emptied after 7 days.
Automatic Date
This is more to save time but is more of a simple PHP function rather than a WordPress specific snippet:
© Copyright 2005 - <?php echo date('Y'); ?>
Insert that into your footer.php and your Copyright date will always be correct.
Easy Random Posts
You can get plugins to display random posts, but this way is quick and easy, you could for example style it up and use in your sidebar.php file:
<h1>Random Posts</h1> <?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php endforeach; ?>
note: you can change the amount of posts by altering numberposts=5.
Sexier Permalinks
This is one I change on EVERY WordPress install I have a hand in, the WordPress default structure doesn’t really look cool, and I’m not keen on the common settings.
So in your permalink settings section add the following in the “Custom Structure” input field:
/%postname%/
And you will end up with a structure link this: http://wpzine.com/post-name
Recent Comments First
WordPress by default displays comments with the newest at the bottom, this is most cases isn’t an issue, but if you run a popular blog new comments could end up a few pages deep in no time.
To combat this you can alter some code in the comment.php page which will show newest at the top.
Find:
foreach ($comments as $comment)
and above it add:
$comments = array_reverse($comments);
Allow Users To Subscribe To Your Twitter Feed
Another method of getting people to read your tweets and perhaps even follow you on Twitter is to allow them to subscribe to your Twitter feed directly from your blog.
So somewhere in header.php between the <head> tags insert:
<link rel="alternate" type="application/rss+xml" title="BLOG NAME Twitter Feed" href="http://twitter.com/statuses/user_timeline/TWIITER USER ID.rss" />
You will need to change the title and insert your Twitter User ID, then your feed will then be offered to users when they click the RSS icon alongside their address bar.
Related Posts Without A Plugin
Sometimes you might not want to use a plugin for this and might well just opt to have it built in to the loop.
Open single.php and paste in the following snippet:
<?php $tags = wp_get_post_tags($post->ID); if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $rel_query = new WP_Query($args); if( $rel_query->have_posts() ) { while ($rel_query->have_posts()) : $rel_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; } } ?>
This is set to show 5 related posts, by editing the line: ‘showposts’=>5, you can alter that amount to suit.