Site logo

Archived topic

Disable header element when no featured image is uploaded

13 replies · Started by George on October 26, 2020

Viewing posts 1–14 of 14

I have set up a header element that is displayed in all posts and pages. The element shows the featured image and the title when a page is loaded and the post title with the post meta and category when a post is loaded. All post meta is being displayed through header element template tags.

I want to be able to programmatically disable the header element whenever a featured image is not present in a post or page. What I mean but that is that I want it to be completely deactivated (not hidden with CSS) so that normal single post meta tags are displayed instead as if the header element was not added.

I tried disabling the Featured Image / Page Header option on the page but that doesn't seem to deactivate the header element.

Yes, Leo, that's EXACTLY what I want! I only had to add ! to the first thumbnail conditional.

Thanks!

No problem :)

Hi Leo, sorry for opening this again. This only works for a post, how should I modify to work for a page as well?

I wonder if this would work:
has_post_thumbnail( $post->ID )

Nope...

What about this _generate-disable-post-image, should it be something else for pages?

Hi,

Try this snippet.

add_filter( 'generate_header_element_display',function($display){

    if( !has_post_thumbnail() || get_post_meta( get_the_ID(), '_generate-disable-post-image', true) === 'true' ){
		$display = false;
		
	} return $display;

},10,1);

Hi Elvin, the last code only works for pages now! This is what I have so far:

add_filter( 'generate_header_element_display', function( $display, $element_id ) {
    if ( 80 === $element_id ) { // Only target specific Element
        if ( !has_post_thumbnail(get_the_ID()) || get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' ) {
            $display = false;
        }
    }
    return $display;
}, 10, 2 );

I have also disabled featured images for single posts and pages from the Customizer.

Only using has_post_thumbnail() in the conditions works, I think the rest of the condition has an issue?

Hi,

I've updated the code from the previous reply.

Can you try and see if it works for you?

Here's a temporary demo of it working. demo site

Hi, apologies, had a setting that was messing it up. This works for me:

add_filter( 'generate_header_element_display', function( $display, $element_id ) {
    if ( 80 === $element_id ) { // Only target specific Element
        if ( !has_post_thumbnail($post->ID) || get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' ) {
            $display = false;
        }
    }
    return $display;
}, 10, 2 );

Basically what Leo said here:
https://generatepress.com/forums/topic/disable-header-element-when-no-featured-image-is-uploaded/#post-1505211

Apologies again, it seems to be working now, thank you both, amazing support as always!

Glad to hear!

I'll get this added to docs :)

This archived topic is closed to new replies.