0

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?

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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