Archive for the ‘Code’ Category

WordPress Tip: How to Easily Customize Excerpt Output

Saturday, November 12th, 2011

Sometimes it’s the simplest things that baffle those of us working with WordPress. My goal on a recent project was to grab snippets of the latest blog entries and display the text without images. That’s easy enough using the default WordPress
< ?php the_excerpt(); ?>
feature, which strips out any tags including images.

Unfortunately, the_excerpt needs a little love if you want to add a nice “Read more…” link or change the number of characters displayed for each post. By default the number of characters is 55. You can search on this until you’re blue in the face because developers have hacked up various solutions to this over the years, but many of them no longer work in WordPress 3+.

No worries, though. All you need to do is add a few filters to your theme’s functions.php file, customize to your liking, and call it a day.

Change the Excerpt Length

function new_excerpt_length($length) {
	return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');

20 is the number of characters in the above example, so change to whatever you’d like.

Change or Remove the Default [...] Text

function new_excerpt_more($more) {
	return '[.....]';
}
add_filter('excerpt_more', 'new_excerpt_more');

If you’d like to get rid of the [...] completely, just remove the brackets and everything in between the ” marks above. You can also customize this filter to insert a nice “Read more” link at the end of your excerpt.

Add a Read More Link to the Excerpt

function new_excerpt_more($more) {
       global $post;
	return '... <a href="'. get_permalink($post->ID) . '">Read the Rest...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

Customize away!

On “AH-HAH” Moments

Thursday, July 22nd, 2010

Running uphill

Staring at website code day in and day out can do some wacky things to a person. Like any line of work that is repetitive at times, an outlet is necessary. Many web developers turn to video games or music. A select few (myself included) follow a sports team or two. A very select few (myself not included) opt for exceptionally tight pants. Anyway, what I’m getting at is that taking a short break from coding has been extremely valuable to me. An approaching deadline and plenty of caffeine also have proven their worth.

It’s amazing what great things have come from determination and a little sleep deprivation. For me it’s always the smallest thing that will keep me up until it’s fixed. A page that is a single pixel off in Internet Explorer 6, a jQuery animation that isn’t as smooth as it should be, or any other garden variety of things most annoying to developers. Did I mention Internet Explorer 6? (Cough) Is this mic even on?

The more annoying or outlandish the glitch, the better it feels when the perfect bit of code is written. Or maybe not just written, but tested and verified to actually be working. That is my ah-hah moment, when the pixels align and Firefox and Internet Explorer agree on my code. It’s the best feeling in the world to get those issues solved, to have conquered the summit, so to speak. Everything after is like running down the side of the mountain. Still have to be cautious and watch the footing, but the momentum is there.

All in a day’s (or night’s) work.