• On a few of our pages – the page titles are so long that they wrap weird in the browser. I would like to specify a line break for certain pages on my site. I’ve tried searching around and there have been fixes suggesting such as setting up a function to look for a pipe (|) in the title and then adding a page break. These fixes didnt seem to work for me – if anyone can get me on the right track I’d appreciate it.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Can you try adding a <br> as the line break and see if that works? Also, there is a “narrow” option in the settings for thinner themes.

    Thread Starter Arex Bawrin

    (@arex-bawrin)

    It just ignored the break tag. I’m cautious about updating the theme option to narrow because I’m not looking for a universal solution for this. It’s just on a few pages where I need to break.

    Moderator bcworkz

    (@bcworkz)

    Did you check the break tag on front end output? (Flushing any caches to eliminate stale data) In its default state, WP displays break tags in the back end so they can be edited. Then on the front end they are honored as HTML and the title breaks where encountered. Themes or plugins can alter this behavior, so it’s not a given.

    The best solution is to undo whatever code is changing default behavior. It’s a matter of identifying the modifying hook and removing it after it is added. Tracking down that code can be a needle in haystack search.

    Replacing a pipe char with a break tag isn’t a bad idea. Along the same line but better is to replace what became of the break tag with an actual break tag. It probably became & lt;br& gt; (without the spaces). Use str_replace() to find such occurrences and replace with the actual tag. This can be done right on the template, replacing the_title(); with $title = get_the_title();. Then do str_replace() on $title before echoing out.

    There may be several templates on which this is required. It may be easier to hook “the_title” filter and do the str_replace() there. Then your code will be applied to all occurrences of the_title() regardless of template.

    Instead of doing str_replace(), you could probably use html_entity_decode() since the breaks were likely converted by use of htmlentities(). This would undo all entity conversions, not just breaks. This may or may not be desirable.

    Thread Starter Arex Bawrin

    (@arex-bawrin)

    @bcworkz thank you for the advice,

    I’m a bit of a noob so can you explain how I can achieve the last two paragraphs in a little more detail? Like which files I need to edit. Sorry!

    Moderator bcworkz

    (@bcworkz)

    Add this to the bottom of your theme’s functions.php:

    add_filter('the_title', function( $title ) {
      return is_admin() ? $title : html_entity_decode( $title );
    }, 99 );

    If what I think is going on is correct, this will allow inserted break tags (and many other HTML tags) to work correctly on the front end.

    You’ll need to reinstate this code after your theme is updated unless you create a child theme and place the code in the child’s functions.php. If you get an unexpected T_FUNCTION error because of this, your PHP version is out of date.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Splitting Title into Two Lines’ is closed to new replies.