WordPress.org

Support

Support » How-To and Troubleshooting » Limit title to 25 characters

Limit title to 25 characters

Viewing 10 replies - 1 through 10 (of 10 total)
  • You do this.

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_titlesmall('', '...', true, '25') ?></a>

    The three dots is what it will trail with… The number is the amount of letters you would like.

    Then you have to add this to your “functions.php file in your theme folder.

    <?php  function the_titlesmall($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_titlesmall', $before . $title . $after, $before, $after);
    		if ( $echo )
    			echo $title;
    		else
    			return $title;
    	}
    }
    ?>

    You just put it at the very start and should work.

    Moderator t-p

    @t-p

    hi recogn1ze,
    Thanks for your help.

    So far I tried atleast three different solutinos, including yours.

    Your solution and others work fine provided the title of the post is in English. But, if the title is in other language (unicode), none of the solutions hold.

    This is what happens: As a result of the specified title length, if a word in the title gets partially trimmed, then i see weird characters (squares)from that poin on. As a result of these weird characters, the validation (w3C) fails as well.

    Any solution for this isse?

    I’m not sure. Sorry.

    Have you tried to explode the title? Not sure if that works in unicode or not.

    http://wordpress.org/support/topic/389529?replies=13

    gregrickaby Seems to have done similar with that method.

    Not cutting the words and showing a count of words would do it I would guess. But that’s above me.

    Moderator t-p

    @t-p

    hi recogn1ze,

    I will go over the thread you suggested and give it a try.
    Thanks for your help. I really appreciate it.

    Hi,

    Check with this function code to limit the characters in title of the post:

    <?php if (strlen($post->post_title) > 35) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 35) . '...'; } else {
    the_title();
    } ?>

    Thanks,

    Shane G.

    Moderator t-p

    @t-p

    Hi Shane, thanks for your help.

    I placed your code in the functions.php, it did not change anything.

    I am sure I am not using it right. could you please guide me exactly how and where I should be placing it. Thanks again.

    recogn1ze,

    This is exactly what I needed – Thanks!

    Just find this code:
    <?php the_title(); ?>
    and replace with this:

    <?php if (strlen($post->post_title) > 35) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 35) . '...'; } else {
    the_title();
    } ?>

    Example:
    Before:
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

    After:

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php if (strlen($post->post_title) > 35) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 35) . '...'; } else {
    the_title();
    } ?>
    </a>

    Visit http://www.anoesys.com to see the result 🙂

    @kentsimba

    Your solution fits me best! Cause I need together of limiting number I need to give a different class for the title so I can make it smaller too (in some parts of my blog it will be better use)!

    The only thing is that I’m getting the same result (reduced text) for both longer than given number and the ones shorter than it!

    Any idea?

    thanks!

    actually my reply should go to @recogn1ze, and the function I’m using is:

    <?php  function the_titlesmall($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_titlesmall', $before . $title . $after, $before, $after);
    		if ( $echo )
    			echo $title;
    		else
    			return $title;
    	}
    }
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Limit title to 25 characters’ is closed to new replies.
Skip to toolbar