• Hey, I’m trying to use the following to limit my post topics to 30 characters, but it doesn’t see to work with the function that retrieves the data.

    <?php
    $post-string = the_title();
    substr($post-string, 0, 30);
    ?>

    Does anyone know how to do this? I would like it to appear like the following:

    “This is a post topic exam…”
    “Here is another post that…” // Cuts off after so many characters and puts three dots.

    Thanks in advance to any advice!

Viewing 4 replies - 1 through 4 (of 4 total)
  • I’d also like to know.

    Cheesy, but it worked in an old theme I was using:

    <?php $shorttitle = substr(the_title('','',FALSE),0,30); ?>
    <?php echo $shorttitle; if (strlen($shorttitle) >29){ echo '&hellip;'; } ?>

    Does anyone know how to this this? The above solution doesn’t work…

    Thanks

    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='&laquo; %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).'&hellip;';}
    	$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 &raquo;', $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).'&hellip;';}
    	$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','&laquo %title',FALSE,'',20);} ?></li>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Using Substr to limit characters in post title.’ is closed to new replies.