WordPress.org

Forums

[resolved] How to limit character on title (13 posts)

  1. botakedan
    Member
    Posted 6 years ago #

    How to limit the character on the_title() to like 40 character only.
    (...like how to the_excerpt() works)

  2. Dagon Design
    Member
    Posted 6 years ago #

    If you pass the FALSE parameter, the value will be returned from the_title, instead of just being echoed. You can just use empty strings for the before and after parameters. Then you can use the PHP function substr.

    <?php echo substr(the_title('', '', FALSE), 0, 40); ?>

  3. botakedan
    Member
    Posted 6 years ago #

    thanks bRo...

  4. Zhen
    Member
    Posted 6 years ago #

    What about if I want to use "..." at the end of the title?

    eg, Hello world!
    change into "Hello..."

    Thanks in advance!

  5. mydesignbytes
    Member
    Posted 6 years ago #

    What about if I want to use "..." at the end of the title?

    <?php echo substr(the_title($before = '', $after = '...', FALSE), 0, 40); ?>

    Unfortunately "..." are characters put at the end of the title, so if your title is more then 40 the "..." fall off. You can use the $before and place something like », but nothing at the end.

  6. texet
    Member
    Posted 6 years ago #

    Unfortunately "..." are characters put at the end of the title, so if your title is more then 40 the "..." fall off. You can use the $before and place something like », but nothing at the end.

    one way around this is to use conditional statements to add a "..." to the end of truncated titles ONLY. This code chops long titles at 40 characters and then appends '...' to the truncated titles only:

    <?php if (strlen($post->post_title) > 40) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 40) . '...'; } else {
    the_title();
    } ?>
  7. branford
    Member
    Posted 5 years ago #

    good info. thanks texet.

  8. bobinder
    Member
    Posted 5 years ago #

    This works well and the code is good but one thing i lose out on is the permalink to the post itself! How do i keep that active link?

  9. Manuel Schmalstieg
    Member
    Posted 5 years ago #

    Great code indeed!

    @bobinder, if you want to keep this title as a link, you would place this:
    <a href="<?php the_permalink(); ?>">
    before the code given by Texet, and you would close the link by placing
    </a>
    at the end.

  10. bobinder
    Member
    Posted 5 years ago #

    thanks mate, that works like a treat! ;)

  11. ebola5114
    Member
    Posted 5 years ago #

    thanks @Texet and @tar.gz for share this too !

  12. Haitham
    Member
    Posted 5 years ago #

    for some weird reason, this is not working for me when it comes to reading my custom field characters, but when i test it myself, it works??

    <?php $arabic = "السلام عليكم يا جماعة الخير"; ?>
    <?php echo strlen(utf8_decode($arabic)); ?>

    the above strlen function enables it to count utf characters that have more than one byte as one character. So above, its count was "27" characters.

    but when I adopt the same idea for my WP template, strlen counts my summary as "5" characters only, when i know that each summary may have 100+ characters. Here's the code:

    <?php $recent = new WP_Query("cat=3&showposts=10"); while($recent->have_posts()) : $recent->the_post();?>
    <?php $image = get_post_meta($post->ID, 'imageThumb', true); ?>
    <?php $imageURL = get_post_custom_values("imageBig"); ?>
    <?php $summary = get_post_custom_values("articleSummary"); ?>
    <div id="articleSnippet">
        <div id="articleImage"><a href="<?php the_permalink() ?>"><img src="<?php echo $imageURL[0]; ?>" width="156" height="78" /></a></div>
        <div id="articleSummary"><a href="<?php the_permalink() ?>"><strong><?php the_title() ?></strong>
        <br /><?php if (strlen(utf8_decode($summary)) > 40) { echo substr(get_post_custom($before = '', $after = '', FALSE), 0, 40) . '...'; } else { echo $summary[0]; } ?></a></div>
    </div>
    <?php endwhile; ?>

    Am I writing this wrong?

  13. Haitham
    Member
    Posted 5 years ago #

    I now know why it is giving me "5" as the return value, because its counting the result, which is "Array". So there is a mistake in the syntax.

    Please help? I have no clue on how to proceed.

Topic Closed

This topic has been closed to new replies.

About this Topic