[Resolved] Suppressing hero on pages without feature image

Home Forums Support [Resolved] Suppressing hero on pages without feature image

Home Forums Support Suppressing hero on pages without feature image

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #979420
    Steven

    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.

    #979449
    Tom
    Lead Developer
    Lead Developer

    Hi there,

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

    Let me know πŸ™‚

    #980030
    Steven

    Yes – if there’s no feature image, the whole element would be removed.

    #980046
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #980162
    Steven

    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?

    #980393
    Tom
    Lead Developer
    Lead Developer
    #982297
    Steven

    Thanks you! That appears to have done the trick πŸ˜‰

    #982482
    Tom
    Lead Developer
    Lead Developer

    Awesome, glad I could help πŸ™‚

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