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.
-
function the_title($before = '', $after = '', $echo = true, $length = false) {
-
$title = get_the_title();
-
}
-
$title = apply_filters('the_title', $before . $title . $after, $before, $after);
-
if ( $echo )
-
echo $title;
-
else
-
return $title;
-
}
-
}
Finally, when in The Loop, call the_title() like this:
-
<?php the_title('', '', true, '40') ?>
Now, the title will be a mere 40 characters long, and the excerpt (using the_excerpt_reloaded()) can be any desired length.



One Comment, Comment or Ping
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.
Jun 3rd, 2007