WordPress.org

Ready to get started?Download WordPress

Forums

How to control the post title length? (11 posts)

  1. topvip
    Member
    Posted 5 years ago #

    I want to let the post title legth not more than 30 words, how to do?

  2. thisisedie
    Member
    Posted 5 years ago #

    So... just don't type more than 30 words.

  3. topvip
    Member
    Posted 5 years ago #

    that is impossible.

  4. jberghem
    Member
    Posted 5 years ago #

    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.

  5. MichaelH
    Member
    Posted 5 years ago #

    This should work in your loop:

    <?php
    $mytitle = get_the_title();
    if ( strlen($mytitle) >30 )
    $mytitle = substr($mytitle,0,30);
    echo $mytitle;
    ?>
  6. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    That's 30 letters, not words.

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

  7. MichaelH
    Member
    Posted 5 years ago #

    Darn, missed that SMALL point ;)

  8. thisisedie
    Member
    Posted 5 years ago #

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

  9. topvip
    Member
    Posted 5 years ago #

    how to use

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

  10. alanlongmore
    Member
    Posted 5 years ago #

    Thank you MichaelH, this worked great for me.

  11. carl-johan
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.