skip to Main Content

I need to display parts of the post title in italic.

So when writing a title like Lorem <i>ipsum</i> dolor sit it should be displayed as

Lorem ipsum dolor sit

We’re using the Classic editor, not Gutenberg, and there is no way we will change this at the moment.

But I do have access to the theme and could change things in here.

Any chance to get HTML codes in the title working?

2

Answers


  1. I have had success in getting WordPress post/page titles to render italics by placing either <i> or <em> tags into the title using the "Quick Edit" feature found on the Posts (or Pages) page. The "Quick Edit" feature appears when you hover over a particular post, and it only lets you edit the post’s main information (title, author, tags, etc.), but for some reason, it will render HTML tags within the title if they are entered there.

    I am not sure why WordPress works the way it does with regard to this, but that’s how I get titles to render HTML tags within themselves. Inserting the tags into the title via the main WordPress editor, as you noticed, doesn’t seem to work.

    Login or Signup to reply.
  2. You can use ** (markdown italic) in your title, and apply the_title filter:

    add_filter( 'the_title', 'my_the_title_filter', 10, 2 );
    
    function my_the_title_filter( $post_title, $post_id ) {
      $post_title = preg_replace('/*(.*?)*/', '<i>$1</i>', $post_title);
      return $post_title;
    }
    

    For example My *italic* title will return My <i>italic</i> title

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search