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 have always the the_title(); tag also includes a custom field called subtitle?

I want to have a title like this: "subtitle-custom-fild: Posttitle"

Like "Cool: The new TV show from Werner Herzog."

in this case, cool would be the custom field value, and "the new TV...." would be the_title().

I am getting this at the moment with the following code:

<h1><?php $values = get_post_custom_values("myCustomField"); echo $values[0]; ?>: <?php the_title(); ?>"><?php $values = get_post_custom_values("myCustomField"); echo $values[0]; ?>
<p><?php the_title(); ?></p></h1>

The Problem is, that google indexes the posts sometimes with the custom field in title, sometimes without it - which in some cases makes no sense. I also gave all possible "title tags" the complete title the same way in the "title=" attribute....

Is there any way to tell wordpress - if theres a custom field subtext - print it always in front of the post_title?

Thank you!

AD

share|improve this question
1  
What theme are you using? Link it please (if custom: show your header.php code). And please also edit your post and format code properly. Thanks. –  kaiser Sep 5 '12 at 15:51
add comment

2 Answers

up vote 3 down vote accepted
<?php
function add_subtitle($title, $id) {
    $subtitle = get_post_meta($id, 'myCustomField', true);
    $new_title = $title;
    if(!empty($subtitle))
        $new_title = $subtitle . ': ' . $new_title;
    return $new_title;
}
add_filter('the_title', 'add_subtitle', 10, 2);

Basically, this uses the the_title filter to add the subtitle to your title. It only adds it if that custom field is available, otherwise, it leaves the title alone.

share|improve this answer
    
+1, I was writing similar answer, but you beat me, so added an alternative method. –  amit Sep 5 '12 at 16:46
    
Using filter is recommended unless you don't want the change globally ( ie - Other plugin/function using the_title() or get_the_title() gets the new formatted title if filter is used ) –  amit Sep 5 '12 at 16:49
    
@amit which is why I did a check for that custom field he is using as the subtitle. If the post doesn't have it, the title isn't effected. –  Manny Fleurmond Sep 5 '12 at 16:54
    
Thank you!! I will give it a try! –  ad2003 Sep 6 '12 at 8:25
add comment

the_title() Accept parameters, we can use them to pass the custom field value to show either before or after the title.

Example -

<?php the_title( get_post_meta($post->ID, 'myCustomField', TRUE) ); ?>

Note - Make sure you set the 3rd parameter as TRUE, which will return a single STRING,

Edit #1-

/*  
 *  The OneLiner ( Note the DOT between two strings ) 
 *  OutPut - Foo: the post title
 */
the_title( get_post_meta($post->ID, 'myCustomField', TRUE) . ': ');

Reference Codex - Function Reference - the title()

share|improve this answer
    
Didn't realize it took arguments like that. Pretty sweet. –  Manny Fleurmond Sep 5 '12 at 16:14
    
Nice - and how can I set that the custom field shows up before the title? And how could I do a ':' betwenn them? –  ad2003 Sep 6 '12 at 14:12
    
great! thanks amit! –  ad2003 Sep 7 '12 at 1:39
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.