Enter HTML into an article title

There are many instances where an author might want or need to add a bit of HTML to an article title. For example, to hard code a break:

<h2>My Story <br />Is Better Than Yours!</h2>

or if a title of the book is contained within:

<h2>I reviewed <cite>A Tale of Two Cities</cite></h2>

Since you cannot use Textile in the title field, you might be tempted to simply insert your HTML, but that would result in this showing up on the page:

I reviewed <cite>A Tale of Two Cities</cite>

and this in the underlying html:

I reviewed &#60;cite&#62;A Tale of Two Cities&#60;/cite&#62;

The Solution

The solution is to:

  1. Use a placeholder for the html elements you want
  2. Use a bit of php to output the title

For our title example, you could enter the title as:

I Reviewed {A Tale of Two Cities}

and then use this in your article form to output the title:

<h2><txp:permlink><txp:php>
       global $thisarticle;
       assert_article();
       $my_title = str_replace('{', '<cite>', $thisarticle['title']);
       echo str_replace('}','</cite>',$my_title);
   </txp:php></txp:permlink></h2>

Using a placeholder is important so that the titles in your RSS feed or output into the html <title> element don’t contain html elements.

Thanks to jsoo on this thread for the tip!

comments powered by Disqus