Advertisement

It’s amazing that in my own non-scientific poll, half of those who work in some technology field still get the term hacker confused. A hacker is not someone who is leering in cyberspace ready to infiltrate their unsuspecting victim’s computer and pillage any information of use from its hard drive. A hacker is someone who looks for ways to manipulate things to make them work better. So when I speak of hacks for WordPress, I am looking at ways to change the code to make the application work better for us, not how to scrape content or steal log in information.

Hacks vs. Plug-ins

There are many different plug-ins available to help manage content on a WordPress blog so why would we want to mess with the code?

For starters, getting our hands dirty with the code of WordPress gives us a much better insight as to how the software works. As we become more comfortable with the source that powers our blog, we gain a better understanding of how to secure it from malicious attacks.

A second reason to avoid turning to a plug-in as a first resort is that an accumulation of plug-ins causes your blog to become bloated. Eventually, this bloat may begin to affect the way your blog loads and delivers to your audience.

Finally, if you pride yourself on going green or you just want to do what you can for the environment, you may be interested to know that code bloat can unnecessarily burden your server’s CPU. Making your server work harder uses more energy. Cooling a hard working CPU uses even more energy. Reducing code bloat saves energy.

Instead of relying on plug-ins, we are going to look at a few hacks that can help better manage a blog’s content. Each one of these hacks requires a bit of PHP knowledge and the ability to upload to your server.

Display a List of Future Posts

One of the most successful techniques television news uses is the teaser. Right before commercial break, they leak bits of information about upcoming stories to keep the viewers interested and glued to their television sets. You can do the same with your blog by letting your audience know about some of the upcoming posts that will soon be published displayed right in your blog’s sidebar.

  1. Create a new post but instead of posting it immediately, schedule it for another date.
  2. From your Dashboard, navigate to Appearance | Editor. From the list of files on the right hand side of your screen, select sidebar.php.
  3. Where ever you would like your future posts to appear, paste the following code:
    <?php query_posts('showposts=10&post_status=future'); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><?php the_title(); ?></h2>
    <span><?php the_time('j. F Y'); ?></span></p>
    <?php endwhile;
    else: ?><p>No future events scheduled.</p>
    <?php endif; ?>
  4. Click on Update File.

Now your blog’s front page will display any future posts. There is one parameter, showposts, that you can change to the number you choose.

Tell Your Posts When to Expire

Just like any food in the cupboard, your posts might get stale after a little while. This next hack allows you to set an expiration date on your posts so that when the time comes, they leave your blog gracefully.

  1. Locate the WordPress loop that begins here:
    <php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    And ends here:

    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>
  2. Replace your loop with the following code:
    <?php
    if (have_posts()) :
    while (have_posts()) : the_post(); ?>
    $expirationtime = get_post_custom_values('expiration');
    if (is_array($expirationtime)) {
    $expirestring = implode($expirationtime);
    }
    $secondsbetween = strtotime($expirestring)-time();
    if ( $secondsbetween > 0 ) {
    // For example…
    the_title();
    the_excerpt();
    }
    endwhile;
    endif;
    ?>
  3. Save your changes.
  4. Write your post and from the custom fields, select the key expiration. Set the expiration date in the following format: mm/dd/yyyy 00:00:00.

Now, when your post hits the expiration date, it will be not be displayed in the loop, therefore, your audience will no longer see it.
In addition to trimming stale posts from your blog, this can be a great solution if you are running a contest and only want content posted for a short time, or if you are advertising a sale and want the related posts cleared from your blog when it is over.