2

I'm using a few functions and conditionals to display page titles and a subpage title field on all pages. Instead of adding this code to the page template, I want to add it to my functions file so I can easily add too the conditional if I’d like. I found this code which successfully adds something to the ‘the_title’ function if on a page, but I’m having trouble applying my code and conditions to it.

Here's my conditional code currently on the page template:

<?php $subtitle = get_post_meta($post->ID, 'html5blank_webo_subtitle', true); ?>
    <?php if ( $subtitle ) : ?>
        <div class="page-title"><?php the_title(); ?></div>
        <h1 class="subtitle"><?php the_subtitle(); ?></h1>
    <?php else : ?>
        <h1 class="page-title"><?php the_title(); ?></h1>
<?php endif; ?>

Here's the code I found and am trying to apply the above code to:

add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
    if('page' == get_post_type($id))
        $title = 'Application has been updated to v'.$title;
    return $title;
}

And here's my failed attempt:

add_filter('the_title', 'new_title', 10, 2);
function new_title($title, $id) {
    if('page' == get_post_type($id))
        $subtitle = get_post_meta($post->ID, 'html5blank_webo_subtitle', true);
    if ( $subtitle ) :
        $title = '<div class="page-title">' . the_title() . '</div><h1 class="subtitle">' . the_subtitle() . '</h1>';
    else :
        $title = '<h1 class="page-title">' . the_title() . '</h1>';
    endif;
    return $title;
}

Final working code:

// Swapping the default 'the_title' with our subtitle on pages
function subtitle_title( $title, $id ) {
    if( !in_the_loop() || !is_page() ) // If not in the loop or on a page, default the the regular 'the_title'
        return $title;

    $subtitle = get_post_meta( $id, 'html5blank_webo_subtitle', true );
    if ( $subtitle ) :
        $title = '<div class="page-title">' 
            . $title
            . '</div><h1 class="subtitle">'
            . $subtitle
            . '</h1>';
    else :
        $title = '<h1 class="page-title">' . $title . '</h1>';
    endif;
    return $title;
}
add_filter( 'the_title', 'subtitle_title', 10, 2 );
Adam Robertson
  • 547
  • 1
  • 9
  • 22

1 Answers1

1

WordPress doesn't have the function the_subtitle, but normally all functions starting with the_* will print the value and functions starting with get_the_* will return the value.

Your code entered an infinite loop, because you're calling the_title (should be get_the_title) inside a function that's filtering the_title. It could be fixed removing and adding the filter inside the callback, but that's not needed, as the title is already available.

And also, you're using $post without it being defined, and you don't need it, the post ID is already available too.

Finally, I'm thinking that you're confusing this the_subtitle() function with the value your getting from $subtitle=get_post_meta().

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

function new_title( $title, $id ) 
{
    # http://codex.wordpress.org/Conditional_Tags
    if( !is_page_template( 'about.php' )  )
        return $title;

    if( 'page' == get_post_type( $id ) )
        $subtitle = get_post_meta( $id, 'html5blank_webo_subtitle', true );

    if ( $subtitle ) :
        $title = '<div class="page-title">' 
            . $title
            . '</div><h1 class="subtitle">'
            . $subtitle
            . '</h1>';
    else :
        $title = '<h1 class="page-title">' . $title . '</h1>';
    endif;
    return $title;
}
brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • 1
    Thanks, I'll try this in the morning. Sorry that I left it out, but the_subtitle is a custom function that I'm using to pull from a custom meta box. I understand the naming convention I used can be confusing, as it appears that I've confused it with something in the core, but it's a legitimate custom function. My original code (the first sample) checks to see if there's anything in the meta box ($subtitle) and if there is, it spits out the function the_subtitle. – Adam Robertson Sep 27 '13 at 05:22
  • Thanks, this worked perfectly! However, every place 'the_title' is called, including in my breadcrumbs and in my navigation, it's being replaced by this new function. Is there any way I can limit it to just the actual page/post title of the page? Thanks again, I really appreciate this. – Adam Robertson Sep 27 '13 at 14:10
  • I missed an important part, you'll have to do a proper check so as not to mess the title everywhere. Answer updated. – brasofilo Sep 27 '13 at 14:25
  • Thanks, this makes sense. But what conditional would I apply? I tried in_the_loop, but that seemed to strip the title from other areas. I'm starting to think I should just wrap my original code in a function and call the function the page templates I want the subtitle version of the title to appear (instead of just the_title). – Adam Robertson Sep 27 '13 at 15:46
  • Maybe [`is_page( array('about','Another One',55) )`](http://codex.wordpress.org/Function_Reference/is_page) – brasofilo Sep 27 '13 at 16:16