Enabling Post Thumbnails in WordPress and Removing Post Revisions

I discovered that the post thumbnail feature, which is now a standard part of WordPress, is missing on my site, but it’s quite a useful feature. Luckily, adding it is simple and doesn’t require any plugins. You just need to tweak the functions.php file and then place the thumbnail code where you want the images to appear.

To enable support for post thumbnails, add the following line to your functions.php file:

add_theme_support('post-thumbnails');

Voilà! You now have the “Set Featured Image” button in your admin panel. Next, you need to display the thumbnail. I want this on my homepage only, as I show only excerpts and titles in categories. So, in index.php (or within the loop if it’s in a separate file), add the following code to display the thumbnail, linking it to the post:

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

This will display the image at the same size it is in the post, which might not always be suitable. You could create custom thumbnails for each post, but another option is to specify the size. WordPress duplicates images in various sizes as defined in media settings (thumbnail, medium, large, full). I prefer the thumbnail size. The first parameter is the image ID, so you need to pull the image from the correct post using $page->ID. The final code looks like this:

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></a>

The image will display with standard attached classes. One of these is attachment-thumbnail, which you can style in CSS. In my case, I will float it left and decide on further styling later.

Another thing to consider: when displaying the post content with the_content, the first image under the heading also appears next to the thumbnail in post lists. The simplest solution is to use the_excerpt, but this might break older posts that I don’t want to touch. Instead, I’ll strip images from the_content output like this:

<?php
$content = get_the_content('read more');
$postOutput = preg_replace('/<img[^>]+./', '', $content);
echo $postOutput;
?>

Removing WordPress Post Revisions

I’ve been using WordPress for years and have never needed post revisions. They tend to clutter the database. To disable this feature entirely, add the following line to the wp-config.php file:

define('WP_POST_REVISIONS', 0);

Mission accomplished for today! Next up, I’ll tackle robots.txt