How do I apply this conditional logic to a complicated page title/subtitle function?

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:

Read More
<?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 );

Related posts

Leave a Reply

1 comment

  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;
    }