Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

How to disable title for posts with a header element

  • I would like to disable the title for all posts that use a header element. I could do this individually or using a layout element. But I would prefer a more automatic solution. I could use CSS and the :has pseudo-class, but this feels a bit hacky. Do you have an idea how to achieve that programmatically?

    Thank you, Mike

  • Hi there,

    You can try this method:
    1. Create a new Layout Element to disable the post title. Do not set any Display Rules, and note its ID.
    2. Add this snippet:

    function my_layout_depends_on_header( $display, $post_id ) {
        // The Layout Element we want to conditionally display.
        $layout_element_id = 2;
        
        // The Header Element that must be present for the Layout Element to display.
        $header_element_id = 1;
        
        // Only modify behavior for our Layout Element.
        if ( $post_id === $layout_element_id ) {
            // Get the Header element's display conditions.
            $header_display_conditions = get_post_meta( $header_element_id, '_generate_element_display_conditions', true ) ?: array();
            $header_exclude_conditions = get_post_meta( $header_element_id, '_generate_element_exclude_conditions', true ) ?: array();
            $header_user_conditions = get_post_meta( $header_element_id, '_generate_element_user_conditions', true ) ?: array();
            
            // Check if the Header element would display on the current page.
            $header_displays = GeneratePress_Conditions::show_data( 
                $header_display_conditions, 
                $header_exclude_conditions, 
                $header_user_conditions 
            );
            
            // Only display our Layout element if the Header element would display.
            return $header_displays;
        }
        
        // Return original value for other elements.
        return $display;
    }
    add_filter( 'generate_element_display', 'my_layout_depends_on_header', 10, 2 );

    Replace the following IDs accordingly:

    $layout_element_id – The ID of the Layout Element you just created.

    $header_element_id – The ID of the Header Element applied to the posts or pages where you want to hide the title.

    Let me know if that works!

  • Sorry for not replying earlier. I didn’t get a notification of your post.

    Your solution totally works, but it has two drawbacks: It works only for one header element that has to be replaced. So, I would need an array of IDs to be replaced.

    In my specific case it could be even more straighforward. The post title should be removed for ALL posts that use a header element.

    I should have added more background information in the original post. The header elements always contain an H1 heading and a background image. Since this is the default layout on pretty much all of my pages that use a header element, a function would be a much better solution than using the layout setting for every page or a specific page template. In fact, this is such a standard scenario that a checkbox in the header settings (to disable the title) would be a great feature. A full-width header image with no text is a rare exception. Since this is the first important text on the page, it makes perfect sense to use an H1 heading.

    Thank you, Mike

  • Hi Mike,

    Why not use a block element – page hero instead of the header element?

    The block element – page hero has a built-in function to disable the default title.

    Let me know πŸ™‚

  • Hi Ying,

    I don’t use the block editor (and I hope, I will never have to). πŸ™‚ I fully understand that you are more focused on developing block elements. On the other hand, I think that a code snippet would be very handy for many users that love GP but not the block editor. I have the feeling that I’m not the only one. πŸ™‚

    Thank you, Mike

  • It works only for one header element that has to be replaced. So, I would need an array of IDs to be replaced.

    If you have multiple Header elements, you can try this snippet instead:

    function my_layout_depends_on_headers($display, $post_id) {
        // The Layout Element we want to conditionally display
        $layout_element_id = 2;
        
        // The Header Elements - layout will display if ANY of these display
        $header_element_ids = array(1, 3, 5); // Add all your header IDs here
        
        // Only modify behavior for our Layout Element
        if ($post_id === $layout_element_id) {
            $any_header_displays = false;
            
            // Check each header element
            foreach ($header_element_ids as $header_element_id) {
                // Get the Header element's display conditions
                $header_display_conditions = get_post_meta($header_element_id, '_generate_element_display_conditions', true) ?: array();
                $header_exclude_conditions = get_post_meta($header_element_id, '_generate_element_exclude_conditions', true) ?: array();
                $header_user_conditions = get_post_meta($header_element_id, '_generate_element_user_conditions', true) ?: array();
                
                // Check if this Header element would display on the current page
                $header_displays = GeneratePress_Conditions::show_data($header_display_conditions, $header_exclude_conditions, $header_user_conditions);
                
                // If any header displays, set our flag to true
                if ($header_displays) {
                    $any_header_displays = true;
                    break; // We found one header that displays, no need to check others
                }
            }
            
            // Display our Layout element if any of the Header elements would display
            return $any_header_displays;
        }
        
        // Return original value for other elements
        return $display;
    }
    add_filter('generate_element_display', 'my_layout_depends_on_headers', 10, 2);
  • Works like a charm. Awesome! Thank you very much!

    Mike

  • Glad to hear that! πŸ™‚

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.