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 just split the_title into two different lines without a custom field?

The titles in my blog are like small header above the maintitle.

The Problem is, that at the moment I am using a custom field in the title - but google and disqus only use "original" title - without a custom field.

So my question - couldn't I just split the_title e.g. into TitlePart One: (header) TitlePart Two: Mainline.

Or can I use some code to make the custom field I am using for the title bring to the "original" title?

share|improve this question
add comment

2 Answers

You could try to achieve this also with another approach: by entering both title and subtitle into the Title field, and separating them with some separation character you consistently use.

Then you would modify your theme ONLY at the places where you want the title to be split on different lines, where you would use something like this:

$mystring = get_the_title();
$findme   = ' - '; // your separator
$pos = strpos($mystring, $findme);
if ($pos === false) {
      $improved_title = '<h1>'.$mystring.'</h1>';
  } else {
      // isolate part 1 and part 2.
      $title_part_one = strstr($mystring, ' - ', true); // As of PHP 5.3.0
      $title_part_two = strstr($mystring, ' - ');
      $improved_title = '<hgroup><h1>'.$title_part_one.'</h1><h2>'.$title_part_two.'</h1></hgroup>';
  }
echo $improved_title;

The great benefit of that approach:

In all the locations where you want both TitlePartOne and TitlePartTwo in the same field (like in the header, post listings, your RSS feed, the title attribute...) you are on the safe side and don't need to do anything.

share|improve this answer
add comment

The best way to go about it is using the the_title filter. Put the following code in your functions.php.

add_filter( 'the_title', 'wpse_64467_prefix_title', '', 2 );
function wpse_64467_prefix_title( $title, $id ){
    $header_title = get_post_meta( $id, 'header_title', true );
    $title = ( $header_title ) ? $header_title . ' - ' . $title : $title;
    return $title;
}

Just replace header_title with the custom field name you are using.

UPDATE: I guess google takes what it gets in the <title> tag in the head as the title. The above code won't modify that. To modify the same you need to use the single_post_title filter in the following manner.

add_filter( 'single_post_title', 'wpse_64467_prefix_wp_title', '', 2 );
function wpse_64467_prefix_wp_title( $title, $post ){
    $header_title = get_post_meta( $post->ID, '_custom_text', true );

    $title = ( $header_title ) ? $header_title . html_entity_decode( ' &#8211; ', ENT_QUOTES, 'UTF-8' ) . $title : $title;
    return $title;
}

P.S - It would take time for the google results to reflect.

share|improve this answer
    
wow this looks nice! I will give it a try - is it possible to get one part in between a "<span>title 2:</span><p>title2</p>"? –  ad2003 Sep 7 '12 at 13:06
    
wrapping it in span would get that in the title attribute of the link too as that also uses the same filter.. a workaround for that is to add/remove two separate filters.. one filter for the title attribute and the other for the normal text. –  Joshua Abenazer Sep 7 '12 at 13:18
    
hmmm...thank you very much, I tried it and it works. Is this getting an "official" posttitle then e.g. for disqus? –  ad2003 Sep 7 '12 at 13:25
    
another question - can I only use your snippet for e.g. the title which is used by disqus, without using it in the single.php - I could just use my old set up with calling the(old)_title + custom field. But the_title would be submitted to google and disqus and so on? Sorry for stupid questions, but I am really not into the title filtering...I will have a closer look now! –  ad2003 Sep 7 '12 at 13:28
    
yes you can say that... any plugin using get_the_title or the_title in its code should be affected by that. You can even set a priority to the filter if required. –  Joshua Abenazer Sep 7 '12 at 13:30
show 3 more comments

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.