- This topic has 10 replies, 4 voices, and was last updated 1 month, 2 weeks ago by
David.
-
AuthorPosts
-
October 14, 2022 at 2:30 am #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?
October 14, 2022 at 2:37 am #2373165Dan
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?
October 14, 2022 at 5:46 am #2373326David
StaffCustomer SupportHi 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 tonull
if the post_generate-disable-post-image
is true.Let me know if that works 🙂
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 14, 2022 at 7:31 am #2373428Dan
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
October 14, 2022 at 7:35 am #2373434David
StaffCustomer SupportAwesome – glad to be of help 🙂
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/February 11, 2023 at 12:36 pm #2529765John
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
February 11, 2023 at 1:17 pm #2529807Ying
StaffCustomer SupportHi John,
Can you make sure the cache is cleared including server-level cache?
And how did you add the PHP snippet?
Let me know!
February 11, 2023 at 1:32 pm #2529819John
Sorry, I should have mentioned it. Cache has been cleared too.
PHP was added using Code Snippet plugin (set to run everywhere)
February 12, 2023 at 4:19 am #2530181David
StaffCustomer SupportHi 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.
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/February 12, 2023 at 8:59 pm #2530939John
David: I just did, thank you
February 13, 2023 at 2:21 am #2531143David
StaffCustomer SupportThanks John
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/ -
AuthorPosts
- You must be logged in to reply to this topic.