Archived topic
Disable Featured Image Not Working In Content Template (even with snippet)
18 replies · Started by John on February 12, 2023
Hi team,
I use GP Premium and GB Pro.
I have created a content template for my Pages. When I use it, the Featured Image always shows up, even if I try to disable it with GP disable elements feature.
I used this snippet from David on another thread:
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 );
Unfortunately it still doesn't work, I don't why. Can you help? Thank you
Hi John,
For that Page, you're using a Block Element - Content Template which has a Block loading the Featured Image. See: https://share.getcloudapp.com/GGuJY9db
You can either delete that or modify it to whatever you prefer.
Hi Fernando,
Yes, and I'm hoping to disable it using the script in the post. My source is https://generatepress.com/forums/topic/disable-elements-featured-image-not-working-in-content-template/#post-2530939
Are you planning to disable it for specific pages only?
Yes, exactly
I believe the easy approach to this would be just to create 2 instances of the Content Template.
The approach you want initially would only work if you already have a custom Post Meta field called: _generate-disable-post-image.
If you do, you can just respect-disable-featured-image to the class list of your Image Block. With that, the PHP should work.
Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Fernando, I'm confused by your response compared to David's in the other thread I linked to. There doesn't seem to be any custom field manually created to make that script work, did I misunderstand?
My assumption was that _generate-disable-post-image was a field generated by GP itself when the Disable Featured Image is checked.
Creating 2 content templates is not an easy approach for me at all... I can't count on my website editors to go manage the Elements display options, however it's much easier for them to check "Disable Featured Image" in the post itself
Oh yes. Thank you for reminding me of that.
That code should indeed work. Just add respect-disable-featured-image to the class list of your Image Block.
Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Yes, I understand it should work :) but it doesn't. Hence this post, I'm trying to resolve why it doesn't work
Can you try creating a Layout Element and disabling the Featured image from there? Reference: https://docs.generatepress.com/article/layout-element-overview/#disable-element
I did and it doesn't disable the Featured Image from the Content Template
Can you try changing the class to page-header-image for in the content template?
Ying, thank you it seems to work! The result is:
- If I disable the Featured Image from the Hero element, it removes the default featured image from the default template, but NOT the featured image from the Content Template.
- If I disable the Featured Image from the page itself, it ALSO removes the featured image from the Content Template.
In the end, it gives me the intended result!
However I'm trying to understand: I disabled the PHP script that David had suggested in the other thread, and your solution still works. So no snippet is needed for this to work? Is page-header-image a default class from the theme?
The Disable featured image option in the post/page editor is using CSS, not PHP.
I read the topic and understand that it's easier for authors to tick that box in the post/page editor.
So as long as the container or image has the page-header-image, it will be hidden by GP's theme CSS.
In this case, no PHP snippet is required. But please be noted, this solution is hiding the image, not removing the image.
Oooh I see. I didn't realize that. That's less than ideal because that image is one of the heaviest file to download on most pages... It's a shame to force users to download it.
Is there any way to make David's script work?