Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way to insert images into a post title? It would also need to display the alt text in the <title> tag, permalinks, etc.

The reason I need to do this is I have a client who want their logo used in page titles instead of plain text. Therefore, the image could be anywhere in the title, so I can't just append/prepend the image.

The only solution I can think of would be to do a find and replace using javascript, but this doesn't seem like a great solution...

share|improve this question
1  
To clarify, they want to use their logo in place of their business name within titles? If this is the case I would look at using a filter - codex.wordpress.org/Plugin_API/Filter_Reference/the_title the function can then replace the specific text with the image tag/information. –  nvwd Jul 30 '12 at 15:15
add comment

1 Answer

up vote 3 down vote accepted

A search and replace solution does sound viable. I wouldn’t do it via JavaScript, though.

add_filter('the_title', 'wpse60174_logo_in_title', 10, 2);

function wpse60174_logo_in_title($title, $post_id)
{
    // Add a <span> around the company name
    return preg_replace('~\bCompany\s+Name\b~i', '<span class="company">$0</span>', $title);
}

With the extra span in place it is just a matter of applying some custom styles to it. You can provide different styles depending on where the title is shown. You may even decide to not replace the company name with a logo image in some place. It’s a flexible setup with clean HTML.

/* A demo CSS rule */
h1 > .company { display:inline-block; background:url(logo.png); width:100px; height:20px; }
share|improve this answer
    
Perfect, thank you! –  slm_92 Jul 30 '12 at 15:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.