0

I am trying to hide the pages H1 header IF a custom field specifies this.

On a page back end I have added a custom field: Name = hide_title Value = true

I then want to hide only the H1 using a filter.

The following code does not work but is the result of my noodling around. Can anyone suggest a fix for this code or an alternate way of approaching this. I do not want to amend the core template files.

Thanks

    // Hide H1 Title

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

    function hide_title($title, $id) {

        global $post;
        $key = 'hide_title';
        $meta = get_post_meta($post->ID, $key, true);

        if ($meta = 'true') $title['post_title'] = '';
        return $title;

    }

1 Answer 1

0

Depending on the way your theme uses the_title, hooking into the the_title filter may or may not hide the title h1. What theme are you using?

The the_title filter passes two arguments: the title (string) and the post ID. Therefore, treating the title as an array (it looks like you're assuming $title is an array of post data, trying to set $title['post_title']. To have the_title return an empty string if the custom field "hide_title" is not empty, the following would suffice:

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

function wpse145940_hide_hidden_title( $title, $postid ) {
    if ( get_post_meta( $postid, 'hide_title', true ) ) {
        $title = '';
    }

    return $title;
}
1
  • Using Woothemes Canvas. Worked perfectly. Made one alteration: if ( get_post_meta( $postid, 'hide_title', true ) == 'true' ) { So that the value of the custom field had to be true (rather than just existing). Thanks
    – ejntaylor
    May 29, 2014 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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