Show custom excerpts of the post in WordPress

When WordPress is used as a CMS, then we no longer wants the homepage to be a chronological order of posts. We would want the homepage to be something which serves the purpose of telling everything about itself. Certainly that should have a section where blog updates are being highlighted either with excerpt or with just titles.

Since in our implementation, we only be having a single section where I would want to use the WordPress loop so we can straight go ahead and use it. Had you been using WordPress loop somewhere else on the page then you would have to use a different object for the WP loop.

Showing links to latest updates from the blog

<?php $count=3; if (have_posts()) : ?>
<ul>
	<?php while (have_posts()) : the_post(); ?>
		<?php if($count > 0) { ?>
		<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
	<?php $count--; } endwhile; ?>
</ul>
<?php endif; ?>

Change the value of count to your choice and there you have a list of latest updates from your blog.

Showing the latest post with an excerpt

I have created a function which you can call anywhere in your theme to do its job of printing the excerpt of the latest post with your specified character limit.

<?php the_post(); ?>
            <h4><a href="<?php the_permalink(); ?>" title="Permanent Link to <?php the_title_attribute(); ?> - Written on <?php the_time('l, F jS, Y'); ?>"><?php the_title(); ?></a></h4>

<?php if ( function_exists( 'ashfame_get_stripped_content' ) ) ashfame_get_stripped_content(300); ?>

You can change the value of 300 to the limit of characters you want to display.

Add this function in your functions.php file (under your theme folder only)

function ashfame_get_stripped_content($limit)
{
	$content = get_the_content();
	$words = explode(" ",$content);
	unset($content);
	$length = 0;
	foreach ($words as $i => $w)
	{
		$length += strlen($w);
		if ($length < $limit)
			$content = $content." ".$w;
		else
			break;
	}
	$content = apply_filters('the_content', $content); // We have to do these 2 steps
	$content = str_replace(']]>', ']]&gt;', $content); // as per the WP Codex when using get_the_content
	$temp = " (<a title=\"Continue Reading..\" href=\"";
	$temp = $temp.get_permalink();
	$temp = $temp."\">Continue reading &#8594;</a>)</p>";
	$content = str_replace('</p>',$temp, $content);
	echo $content;
}

Showing multiple posts with an excerpt

You will have to execute the WordPress loop here to access multiple posts. You can show multiple posts with an excerpt by calling the function of above case inside the loop like this :

<?php $count=3; if (have_posts()) : ?>
<ul>
	<?php while (have_posts()) : the_post(); ?>
		<?php if($count > 0) { ?>
		<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
		<?php if ( function_exists( 'ashfame_get_stripped_content' ) ) ashfame_get_stripped_content(300); ?>
	<?php $count--; } endwhile; ?>
</ul>
<?php endif; ?>

Change the value of count to your choice and there you have multiple posts each with an excerpt. Have any questions then use the Comments section below. Make sure you check out the whole WordPress as CMS series.


Comments

4 responses to “Show custom excerpts of the post in WordPress”

  1. Currently, I’m displaying only post titles on my front page. Any way to ajax-ify the excerpt below the titles? The default would be hidden and upon clicking a tiny (+) link, it shows the excerpt below the title. Like on my Movie Reviews page (use a plugin so dunno how to replicate).

    1. I am sorry that I missed your comment somehow. Just noticed it. What you want is a JS solution which can hide a DIV content and then show it on a click. You don’t need AJAX. Try searching for a jquery plugin for the same and implement it in the code to match the DIV name containing the excerpt.

  2. […] Showing posts or custom excerpts of posts […]

  3. These were very helpful, thanks!

    Using your “get_stripped_content” function, how would I strip images out of the excerpt before it starts counting words?