Worked like a charm for me. I needed to truncate the title for use in previous_post_link() and next_post_link() and was trying in vain to use %title in the substr() function. Using the_title should have been more obvious, but it took me a bit of searching to find this thread and hit myself on the forehead. Since the_title will always return the current post, I added a $level argument to the previous_post_link() and next_post_link() functions and a conditional line to do the substring code above. This is admittedly shortsighted; as soon as the next wp build comes out, it’s gone, so I need to migrate the code to a theme file, but for now, here’s what I did, for those interested. (This is my first time posting code, so I hope it doesn’t blow up.)
function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '',$limit = -1) {
if ( is_attachment() )
$post = & get_post($GLOBALS['post']->post_parent);
else
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
if ($limit>-1) {$title = substr($title,0,$limit).'…';}
$string = '<a href="'.get_permalink($post->ID).'">';
$link = str_replace('%title', $title, $link);
$link = $pre . $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
}
function next_post_link($format='%link »', $link='%title', $in_same_cat = false, $excluded_categories = '',$limit = -1) {
$post = get_next_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
if ($limit>-1) {$title = substr($title,0,$limit).'…';}
$string = '<a href="'.get_permalink($post->ID).'">';
$link = str_replace('%title', $title, $link);
$link = $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
}
Then in my theme header, I’m calling the functions with the additional argument to define my desired trim length:
<li><?php if (is_single()) {previous_post_link('%link','« %title',FALSE,'',20);} ?></li>