Add a line break in a WordPress post title

Imagine your client wants to have a line break in a WordPress post title. The requirement seems simple but is equally tricky. But by this simple trick you can add desired number of line breaks in your post title.

Also, sometimes a post title breaks in an odd way. Suppose 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.

Technically, 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?

#Trick

The trick involves inserting a special character into the title where you want the break, for example a vertical pipe “|” as the indicator. The title should be then entered like this:

This is a | title with a | custom break

WordPress is smart enough to not include special characters in the URL, so all that’s left is making our template swap the vertical pipe with a line break.

#Use str_replace

Normally you use the_title() to display the post title and you are done. In this case, we want to search the title string for vertical pipes before we output it.

Something like this should do the trick:

<?php echo str_replace(' | ', '<br />', get_the_title()); ?>

That str_replace function accepts three arguments, the value to search for, the value to replace it with when found, and the subject string (in this case, our title) to operate on.

Our function finds vertical pipes in the title and replaces them with a <br>before outputting.

#Alternate Way

Alternate way is to use a plugin. Install and activate the Page Title Splitter plugin.

Upon activation, you need to edit a post or page where you want to split the title. On the post editor screen, you need to click on the little button below the title field.

Clicking on the button will add a pointer below the title field. You will need to click on the pointer to select it.

After that you can move the cursor inside text field to the point where you want to split the title.

Leave comment

Your email address will not be published. Required fields are marked with *.