Remove Page Title from Static Frontpage

I’d like to remove the post title from my static frontpage. Just that page.
I’ve created a child theme from a free Press75 theme. So I’d like to make this change in my child theme’s function.php.

I used this code but it removes the title from all pages, posts and my navigation:

Read More
add_filter( 'the_title', 'remove_page_title' );

function remove_page_title() {
    if(is_page('138')) {
    }
    return '';
}

I thought it would just remove the title from the page with ID 138.
What am I doing wrong?

Thanks!

Related posts

Leave a Reply

5 comments

  1. wrap your add_filter function call around a conditional tag and use the builtin return false function:

    if ( is_page('138') )
    {
          add_filter( 'the_title', '__return_false' );
    }
    
  2. You’ve almost got it!

    Here’s your original code:

    add_filter( 'the_title', 'remove_page_title' );
    
    function remove_page_title() {
       if(is_page('138')) {
       }
       return '';
    }
    

    What it’s doing is adding a filter on the_title that doesn’t accept any parameters. That’s your first mistake. The the_title filter actually takes two parameters – the page/post title and the page/post id. So first, let’s rewrite our hook:

    add_filter( 'the_title', 'remove_page_title', 10, 2 );
    function remove_page_title( $title, $id ) {
    
    }
    

    This assigns our filter with a priority of 10 and lets us accept both parameters sent in by the filter. Then we specify the parameters our function accepts.

    Inside the function, we want to check if we’re on the right page. If we are, return '', if we aren’t return the title:

    if( '138' == $id ) return '';
    
    return $title;
    

    Your original function was always returning '' when the filter ran. That’s why you were getting a blank title on every page, not just the page you wanted.

    Our full code

    So your full filter, rewritten to accept parameters and use the proper logic:

    add_filter( 'the_title', 'remove_page_title', 10, 2 );
    
    function remove_page_title( $title, $id ) {
        if( '138' == $id ) return '';
    
        return $title;
    }
    
  3. This is way how to remove main title heading (not tag) from front page in 2020 while not removing the_title from menu items too:

    function no_title_front_page( $title, $id = null ) {
        if (is_front_page() && !is_null( $id )) {
            return '';
        }
        return $title;
    
    }
    
    add_filter( 'the_title', 'no_title_front_page', 10, 2 );
    
    
    function wpse309151_remove_title_filter_nav_menu( $nav_menu, $args ) {
        // we are working with menu, so remove the title filter
        remove_filter( 'the_title', 'no_title_front_page', 10, 2 );
        return $nav_menu;
    
    }
    
    // this filter fires just before the nav menu item creation process
    add_filter( 'pre_wp_nav_menu', 'wpse309151_remove_title_filter_nav_menu', 10, 2 );
    
    function wpse309151_add_title_filter_non_menu( $items, $args ) {
        // we are done working with menu, so add the title filter back
        add_filter( 'the_title', 'no_title_front_page', 10, 2 );
        return $items;
    
    }
    
    // this filter fires after nav menu item creation is done
    add_filter( 'wp_nav_menu_items', 'wpse309151_add_title_filter_non_menu', 10, 2 );
    

    Code based on this answer.

  4. If I understand you correctly — that you want to display no <title> for the front page — you can use:

    <title><?php if ( ! is_front_page() ) { the_title(); } ?></title>