Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I searched for "title length" in questions but results were all about setting max length and limiting and..

What I want to do is to create an if-statement, to determine if the post title has more than 68 characters or not and if it had, add smaller class to it.

I wrote the code like this:

<?php
    $permaLength = strlen(the_title());
?>
<h2 class="<?php if ($permaLength > 68) {echo 'smaller';} ?>">
    <a href="<?php the_permalink() ?>">
        <?php the_title(); ?>
    </a>
</h2>

but it seems that the_title() function is not returning the value and instead it's echoing out. Is there an alternative way to fetch the title length?

share|improve this question
    
Don’t use strlen(), use mb_strlen() instead. –  toscho Oct 26 '12 at 12:26
    
'but it seems that' - it would take less than 5 minutes to find out for sure, by searching the Codex; codex.wordpress.org/Function_Reference/the_title –  Michael Oct 26 '12 at 12:41
add comment

1 Answer

up vote 1 down vote accepted

the_title() echoes the result. Use get_the_title() to return the title.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.