[Resolved] Disable Elements – Featured Image – Not Working In Content Template

Home Forums Support [Resolved] Disable Elements – Featured Image – Not Working In Content Template

Home Forums Support Disable Elements – Featured Image – Not Working In Content Template

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #2373159
    Dan

    Hey, I’ve created a Content Template for Single Posts. In it, I’ve added the Post Featured Image block and toggled the ‘Link to element’ settings, as seen here: https://capture.dropbox.com/m6IGqbRHppjfHT4w.

    When editing a post, within the sidebar ‘Layout’ settings, under ‘Disable Elements’, I’ve checked the ‘Featured Image’ to remove it. However, the featured image is still displayed on the post? The remove featured image setting is applied if I change the ‘Content Template’ to draft and view the post with the default template.

    Does the ‘Link to element’ setting in the Content Template not allow for the ‘Disable Elements’ settings to be applied on single posts?

    #2373165
    Dan

    Sorry, I’ve just realised the ‘Link to element’ setting literally means – create a link to the element. And has nothing to do with the GP Disable Elements settings.

    Is there a way I have the featured image within a Content Template respect the Disable Elements settings?

    #2373326
    David
    Staff
    Customer Support

    Hi there,

    the Disable Elements feature will only work with the default Themes featured image.
    However that boolean value is stored in the post meta, so with a little PHP we can do:

    
    add_filter( 'render_block', function( $block_content, $block ) {
    
        if ( 
            !is_admin()
            && is_single()
            && ! empty( $block['attrs']['className'] ) 
            && 'conditional-image' === $block['attrs']['className']  
            && has_post_thumbnail() 
            && get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' 
        ) {
    		
            $block_content = null;
    
        }
    
        return $block_content;
    
    }, 10, 2 );

    In this line:

    && 'conditional-image' === $block['attrs']['className']

    We specify the CSS class of conditional-image ( you can change to whatever you want ) which you can add to the Blocks > Advanced Additional CSS Classes. Then that blocks content will be update to null if the post _generate-disable-post-image is true.

    Let me know if that works 🙂

    #2373428
    Dan

    Hi David,

    Thanks for the response; this looks like a nice solution. I went a different route because I have some conditional blocks already in place, but the get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' was exactly what I needed, thanks.

    Dan

    #2373434
    David
    Staff
    Customer Support

    Awesome – glad to be of help 🙂

    #2529765
    John

    Hi David,

    Thank you for this snippet, it’s exactly what I need. However it’s not working for me.

    Here is my snippet:

    add_filter( 'render_block', function( $block_content, $block ) {
    
        if ( 
            !is_admin()
            && is_single()
            && ! empty( $block['attrs']['className'] ) 
            && 'respect-disable-featured-image' === $block['attrs']['className']  
            && has_post_thumbnail() 
            && get_post_meta( get_the_ID(), '_generate-disable-post-image', true ) === 'true' 
        ) {
    		
            $block_content = null;
    
        }
    
        return $block_content;
    
    }, 10, 2 );

    And I’ve added the class to the container for the featured image in my content template. It’s still showing up after I check “Disable featured image”. Any idea?

    Thank you

    EDIT: I checked that the class is correctly added in the front end

    #2529807
    Ying
    Staff
    Customer Support

    Hi John,

    Can you make sure the cache is cleared including server-level cache?

    And how did you add the PHP snippet?

    Let me know!

    #2529819
    John

    Sorry, I should have mentioned it. Cache has been cleared too.

    PHP was added using Code Snippet plugin (set to run everywhere)

    #2530181
    David
    Staff
    Customer Support

    Hi there,

    can you share a link to where i can see the issue ? If want to do that privately, then please raise a new topic where you can access the private info field.

    #2530939
    John

    David: I just did, thank you

    #2531143
    David
    Staff
    Customer Support

    Thanks John

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