Site logo

Archived topic

Suppressing hero on pages without feature image

7 replies · Started by Steven on August 8, 2019

Viewing posts 1–8 of 8

I see the option for a fallback image if there isn't a feature image associated on a page that has a hero Element applied. However, I'm wondering if there's a way to have the space the hero takes up suppressed if no feature image is selected for a particular page. If not, it would be great to have that as an option on the Appearance > Elements > Page Hero settings page or have this be the default behavior. I know you can use the Element display rules, but that will get out of hand with 2,000+ pages on our site.

Hi there,

What do you mean by suppressed exactly? Like remove the entire Element if no featured image exists?

Let me know :)

Yes - if there's no feature image, the whole element would be removed.

Try this:

add_filter( 'generate_header_element_display', function( $display, $element_id ) {
    if ( 123 === $element_id ) { // Only target specific Element
        if ( ! has_post_thumbnail() ) {
            $display = false;
        }
    }

    return $display;
}, 10, 2 );

Just update 123 to the ID of your Element.

Let me know :)

I'm still new to WordPress so might not be adding this filter correctly. I created a child theme and placed the following in the child theme functions.php file:

<?php
add_filter( 'generate_header_element_display', function( $display, $element_id ) {
    if ( 11089 === $element_id ) { // Only target specific Element
        if ( ! has_post_thumbnail() ) {
            $display = false;
        }
    }

    return $display;
} );

When I do this, all of the site pages are blank. When I remove all content from the child theme functions.php file, the site renders as expected while the child theme is still active. So I think the child theme is set up correctly.

Am I missing a step?

Thanks you! That appears to have done the trick ;)

Awesome, glad I could help :)

This archived topic is closed to new replies.