Speed Tip: How To Shorten Posts On Your WordPress Front Page

How to shorten posts on your wordpress front pageI mentioned in my previous article that I use Homepage Excerpts with a few modifications to load up posts on my main page to keep the load time low.

There’s plenty of reasons not to load full posts on the main page of your blog, but the main one is speed. One way to fix long page load times is by using excerpts.

The code that the boys from Daily Blog Tips have put out is pretty lean and clean already which is why I haven’t bothered just coding this in myself.

Thumbnail Mod For Homepage Excerpts Plugin

The main mod that I’ve done to their plugin code is to enable thumbnail pictures on posts on the front page.

To add thumbnails to the post on the front page using Homepage Excerpts plugin just replace this line of code on line 129 (or there abouts):

$output = $output.$homepage_excerpts_more;

With this:

$output = get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ) . $output . $homepage_excerpts_more;

Shortening Posts Manually

To achieve the same shortening of posts to a custom length on your front page without a plugin like Homepage Excerpts and without using the_excerpt() you can code it directly into your functions.php file by using the following code:

function homepage_shorten_posts_filter($content) {
	global $post; // get the global post object
	$output = $content;
 
	if (is_front_page()){
		$read_more_link = ' [...]
 
<a class="more-link" rel="nofollow" href="https://www.codemyownroad.com/speed-tip-how-to-shorten-posts-on-your-wordpress-front-page/"><strong>Click Here To Read More ...</strong></a>';
 
		$num_words = 100; // number of words you want to shorten posts to
 
		// remove elements we don't want to appear on the front page like javascript, etc
		$output = preg_replace('@<!-- ]*?>.*?
// -->@si', '', $post->post_content);
		$output = preg_replace('@<!--[\s\S]-->@', '', $output);
		$output = strip_tags($output);
 
		// make an array of the words so we can count
		$words = explode(' ', $output, $num_word);
 
		// find out how many words there are
		$word_count = count($words);
 
		// get rid of excess words
		if ($word_count >= $num_word) {
			array_pop($words);
			$output = implode(' ', $words);
		}
 
		// add the thumbnail
		$output = get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ) . $output . $read_more_link;
	}
 
	return $output;
}
 
add_filter('the_content', 'homepage_shorten_posts_filter', 0); // add the filter to shorten posts for the homepage

It’s basically a trimmed down version of the homepage excerpts plugin. Feel free to modify. Adjust the num_words variable to the length you’re comfortable with.

Using  the_excerpt()

You can also achieve the exact same thing with the below code using the_excerpt() and playing around with that. This might be easier to understand, but I like the finer grained control using the above method.

Add this code to your functions.php:

function new_excerpt_thumbnail_filter($post) {
	$output = $post;
	$output = get_the_image( array( 'custom_key' => array( 'Thumbnail', 'thumbnail' ), 'default_size' => 'thumbnail' ) ) . $output;
	return $output;
}
 
function new_excerpt_more($post) {
	return '<a href="'. get_permalink($post->ID) . '">' . '
 
<strong>Continue reading ...</strong>
 
' . '</a>';
}
 
function new_excerpt_length($length) {
	return 100; // default is normally 55
}
 
add_filter('the_excerpt', 'new_excerpt_thumbnail_filter', 0);
add_filter('excerpt_more', 'new_excerpt_more');
add_filter('excerpt_length', 'new_excerpt_length');

You simply need to replace the_content() with the_excerpt() in your home.php file (make sure you’re using a child theme to override this file).

I’ll be back soon with a full list of things you can do to your WordPress website to cut down page load times on your front page and post pages.

Josh Kohlbach

Josh is a software entrepreneur from Brisbane, Australia. He spends most of his time helping e-commerce store owners. This is his personal blog where he shares his thoughts and other tidbits on online business and life in general.

This Post Has 3 Comments

  1. Thanks for the trick, it did it for me. I don’t know of any wordpress settings to accomplish this as the above user mentions

Leave a Reply

Close Menu