WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to limit character on title

[Resolved] How to limit character on title

  • botakedan

    @botakedan

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

Viewing 12 replies - 1 through 12 (of 12 total)
  • Dagon Design

    @dagon-design

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

    botakedan

    @botakedan

    thanks bRo…

    What about if I want to use “…” at the end of the title?

    eg, Hello world!
    change into “Hello…”

    Thanks in advance!

    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.

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

    good info. thanks texet.

    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?

    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.

    thanks mate, that works like a treat! 😉

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

    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?

    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.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Resolved] How to limit character on title’ is closed to new replies.
Skip to toolbar