WordPress.org

Ready to get started?Download WordPress

Forums

Limit title to 25 characters (11 posts)

  1. Tara
    Member
    Posted 4 years ago #

    Hi all,

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

    How can limit the title to say 25 characters?

    I got this code from: http://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    Thanks for your help.

  2. recogn1ze
    Member
    Posted 4 years ago #

    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.

  3. Tara
    Member
    Posted 4 years ago #

    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?

  4. recogn1ze
    Member
    Posted 4 years ago #

    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.

  5. Tara
    Member
    Posted 4 years ago #

    hi recogn1ze,

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

  6. Shane G.
    Member
    Posted 4 years ago #

    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.

  7. Tara
    Member
    Posted 4 years ago #

    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.

  8. WPExplorer
    Member
    Posted 4 years ago #

    recogn1ze,

    This is exactly what I needed - Thanks!

  9. kentsimba
    Member
    Posted 3 years ago #

    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 :)

  10. iansampaio
    Member
    Posted 3 years ago #

    @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!

  11. iansampaio
    Member
    Posted 3 years ago #

    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;
    	}
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic