willoller.com

Avatar

Learning to match the beat of the Old World man.

Limiting title and excerpt length with Wordpress

Sometimes clients have very specific requirements. Sometimes they want autometic controls on simple things they have total control over anyway. I digress.

In this case, blog titles needed to be of any length, but have their lengths truncated under certain circumstances. Furthermore, excerpts need to be able to be the default length (50 words), but shorter (10 words) under those same certain circumstances.

Fortunately for me, the excerpt was easily fixed using a plugin called the excerpt reloaded.

http://guff.szub.net/2005/02/26/the-excerpt-reloaded/

Next, for the title length requirements, you will need to modifiy a core file: wp-includes/post-template.php.

[php]
function the_title($before = ‘’, $after = ‘’, $echo = true, $length = false) { $title = get_the_title(); if ( $length && is_numeric($length) ) { $title = substr( $title, 0, $length ); } if ( strlen($title) > 0 ) { $title = apply_filters(‘the_title’, $before . $title . $after, $before, $after); if ( $echo ) echo $title; else return $title; }
}
[/php]

Finally, when in The Loop, call the_title() like this:

[php]< ?php the_title('', '', true, '40') ?>[/php]

Now, the title will be a mere 40 characters long, and the excerpt (using the_excerpt_reloaded()) can be any desired length.

del.icio.us:Limiting title and excerpt length with Wordpress digg:Limiting title and excerpt length with Wordpress reddit:Limiting title and excerpt length with Wordpress

One Comment, Comment or Ping

  1. Rob

    Found a bug in The Excerpt Reloaded, and I have a fix for it. This bug causes invalid XHTML to be generated. See http://robsnotebook.com/the-excerpt-reloaded for the fix and an explanation of why there is a bug.

Comments are closed. Spammers fuck off.

Before you go