Better Display Of Images In your WordPress Post Excerpts

by Costa on November 18, 2009

wp-logo

Many Wordpress users have problems displaying images in their WordPress blog if they decide to use post excerpts instead of full post in their home page. There are also many solutions and plugins available to solve this problem. In my previous post where I discussed inserting images in your post excerpts using the custom field, users can upload different images for their homepage and their singles post, however this involves some extra work which a busy lazy fellow like me can do without. Thus I looked for some short cut where I can have the exact looking image displayed in my homepage post excerpts.

The codes I found involves overwriting the default WordPress codes that specifies how post excerpts works. Can’t remember where I found them though, but I am real happy with the results. I have deleted the line of code that makes WordPress strip off all html formatting as I want all the formats and image intact. If you want to try it out, here are the codes to paste in your functions php.

Do be reminded to keep a backup of your original functions php file as this can be somewhat sensitive. One missing tag and your whole blog will not load and you won’t be able to access your dashboard to correct things. If this happens, do not panic. Just use your FTP client to delete the functions php on your server, then replace it with your original copy. Remember you must delete the corrupted functions php file, overwriting it will not do any good. So if you are adventurous and have no fear of threading into the unknown, here are the codes. Will be a piece of cake for those with experience though.

function my_wp_trim_excerpt($text) { // Fakes an excerpt if needed

if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$excerpt_length = 80;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, ' ... <a href="'. get_permalink() . '"><div class="read-more">CONTINUE READING</div></a>');
$text = implode(' ', $words);
}
}
return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'my_wp_trim_excerpt');

?>

Copy and Paste the whole bunch onto your functions php file. Remember to check if you have the closing php (?>) tag at the end. Almost everytime I screw up is because I forgot to close the php codes.

After you have updated, if you haven’t done so previously, go to your Index php and look for this line.

<?php the_content('Read the rest of this entry &raquo;'); ?> (Some themes will not, or have a different sentence than this ‘Read the rest of the entry’ )

Replace that line with this:

<?php the_excerpt ?>

Now you have post excerpts that shows everything as it is, including your image. links or whatever html tags that you have used in the first 80 words. This again proves to be a not very professional way as it cuts off words midway as soon is the specified number is up. Tried to hunt for ways to overcome this with codes, but was unsuccessful so I used a plugin called Fancy Excerpt. This plugin will finish the whole sentence instead of cutting off the post midway.

Seems to be a lot of work doesn’t it? Actually it will not take you more than 10 minutes. If however even 10 minutes is too much for you, then spend some money to buy a Theme that just that. The Thesis Theme has a fantastic option that simplifies everything. Not willing to do that? Then wait for WordPress 2.9 which is just round the corner. It has a kickass post image feature that will make life simple for everyone. Justin Tadlock has the news and tips on how you can upgrade you Theme to take advantage of this feature when the big update comes.

Related posts:

  1. Display Thumbnail Images In Post Excerpts Maybe for some reasons or another, you do not like to use plugins to display images in your post excerpts. Using plugins do have limitations. For one, you cannot control the images to be displayed and the post the images to be displayed in. Doing it manually lets you...
  2. Insert Thumbnail Images In Your Homepage Post Excerpts Under normal circumstances, if you are not using a theme that comes with the the Customs Filed functions written in, you cannot insert images to your homepage excerpts. Many bloggers likes to show just a small excerpt of their post in the front page by inserting the “read more” command...
  3. Wrapping Text Around Your Thumbnail Images WordPress 2.9 latest feature that lets users post thumbnail images in their post excerpts has certain limitations which makes me refrain from using this feature. This new feature however will be great if you are planning to use your Theme like a magazine style layout with thumbnail images fixed on...
  4. WordPress 2.9 Image Thumbnails And New Features Just logged in into my Admin panel and saw that WordPress 2.9 is available for download. Upgraded using the most convenient method, that is via the automatic upgrade. Took less than 5 seconds for the upgrade to complete. Read more on WordPress 2.9 Image Thumbnails And New Features… ...
  5. Add Text And Image Shadows To Your WordPress Post To add shadows to your text in your WordPress blog, all you need is a simple line of code. It works with all major browsers except Internet Explorer, which not too surprising. A lot of my modded themes appear different in that silly browser. Even Google Chrome, being a...

{ 2 comments… read them below or add one }

Jamy November 18, 2009 at 6:27 pm

Yes it seems that Wordpress v2.9 is about to be released. Hope it could be in a week or so as I have to start another project.
I need to use thumbnails and I would prefer not to use any plugins or other temp solution.
But what about all these 2.8 themes using plugins like “Get the image” ? Don’t they need to be re-written for 2.9 in order to work straight away with auto thumbnails generation in 2.9 ?
Can someone tell me more about that?

THANKS!!!

Reply

Costa November 19, 2009 at 9:11 am

Hey Jamy,

I’m sure the developers of those plugins have already started checking for compatibility issues and updates should be out in time to avoid any major conflicts, if there is any. However it will be wise to deactivate all plugins first before upgrading. After you have upgraded, activate them one by one so you will know which is giving you problems.

Anyway, with the new feature in WP 2.9, I don’t think you will need plugins to display images in your homepage excerpts anymore.

Reply

Leave a Comment