[Resolved] Disable header element when no featured image is uploaded

Home Forums Support Disable header element when no featured image is uploaded

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1505174
    George

    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.

    #1505182
    Leo
    Staff
    Customer Support
    #1505189
    George

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

    Thanks!

    #1505196
    Leo
    Staff
    Customer Support
    #1505202
    George

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

    #1505211
    Leo
    Staff
    Customer Support

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

    #1505213
    George

    Nope…

    #1505221
    George

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

    #1505241
    Elvin
    Staff
    Customer Support

    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);

    A wise man once said:
    "Have you cleared your cache?"

    #1505244
    George

    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.

    #1505251
    George

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

    #1505259
    Elvin
    Staff
    Customer Support

    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

    A wise man once said:
    "Have you cleared your cache?"

    #1505271
    George

    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!

    #1505272
    Leo
    Staff
    Customer Support

    Glad to hear!

    I’ll get this added to docs πŸ™‚

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