Sometimes a post title breaks in an odd way. Say you have a long title, and you want to break it into two lines. This little snippet will show you how to add a line break
<br> in the title of your WordPress post or page.
Your title is too long, or it breaks in odd places.
Technically speaking, you can add a
<br /> tag right in the title, and WordPress will detect it as a line break. But what if we want the line break in the
single.php template and not in the
archive.php template? This is what we’ll have.
We can give WordPress the ability to detect a special character in
the_title. Choose a special character that you don’t normally use in a title; a pipeline
| for example. Then, we can simply use a function to replace the pipeline character with a line break tag for the
single.php. And use the same function to replace the pipeline with a blank space for the
<?php echo str_replace(' | ', '<br />', get_the_title()); ?>
<?php echo str_replace(' | ', ' ', get_the_title()); ?>
Published by Haneen Krimly