Viewing 10 replies - 1 through 10 (of 10 total)
  • So… just don’t type more than 30 words.

    Thread Starter topvip

    (@topvip)

    that is impossible.

    No it’s not, once you hit 29 words just stop!

    The title is just that a title, not a paragraph. If you’re hoping people will read your posts or that Google will actually index your posts and place them above page 10 of the search results, you’ve got to keep your titles short and concise.

    Writing an article for the Internet needs a completely different approach than writing an article for a newspaper or magazine. People need to see in seconds what the post or article is about. If they don’t they click on the fist link to the next site. If a title is more than 6 words most people don’t even bother reading it.

    This should work in your loop:

    <?php
    $mytitle = get_the_title();
    if ( strlen($mytitle) >30 )
    $mytitle = substr($mytitle,0,30);
    echo $mytitle;
    ?>

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    That’s 30 letters, not words.

    And I agree with the original answer. Stop typing extra long titles.

    Darn, missed that SMALL point ??

    What do you mean it’s impossible? Are WordPress post titles addictive or something? Should there be a warning label? Do you need group therapy to stop?

    “Hi my name is topvip and I’m a WP post title addict. I try and try not to type more than 30 words but my fingers keep right on typing without my consent until there’s 40, 50, and sometimes 60 words!”

    Thread Starter topvip

    (@topvip)

    how to use

    <?php
    $mytitle = get_the_title();
    if ( strlen($mytitle) >30 )
    $mytitle = substr($mytitle,0,30);
    echo $mytitle;
    ?>

    Thank you MichaelH, this worked great for me.

    Thanks Michael, worked great for me too.
    I did some minor changes though, I’ll share them if anyone is looking for the same function I was (getting titles under 40 characters);

    First I added a $after variable so that truncated titles gets whatever you add after the truncation (in my case 3 dots ‘…’)

    Then I noticed that adding these lines to my markup gave all the titles an extra whitespace before the title, so I converted it all into a function which I added to my functions.php and now I just need to call <?php short_title(); ?> in my template.
    I know I could probably just have played around with the linebreaks and found the whitespace but this also has the advantage that it is easy to use where ever I want shorter.

    Also added an $length variable so the final function looks like this:

    function short_title($after = '', $length) {
    	$mytitle = get_the_title();
    	if ( strlen($mytitle) > $length ) {
    	$mytitle = substr($mytitle,0,$length);
    	echo $mytitle . $after;
    	} else {
    	echo $mytitle;
    	}
    }

    Call from the template:
    <?php short_title(‘…’, 40); ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to control the post title length?’ is closed to new replies.