Archived topic
featured-image-active class still included in body when it shouldn't
22 replies · Started by George on November 8, 2020
When disabling the featured image from the page or post through the Disable Elements section, the featured-image-active class is still being added to the body. As a result, it creates confusion in cases when I want to style the page when the featured image is not present. Is that a bug?
Hi George,
Looks like it.
I'll confirm with Tom and report back :)
Ok, thanks Leo. If it's a bug, I would appreciate it if Tom provided the code that he's going to use for the update as I need to finish a project and I need the functionality. Unless there is another way.
Hi there,
Try this:
add_filter( 'body_class', function( $classes ) {
if ( is_singular() ) {
$disable_featured_image = get_post_meta( get_the_ID(), '_generate-disable-post-image', true );
if ( $disable_featured_image ) {
$classes = array_diff( $classes, array( 'featured-image-active' ) );
}
}
return $classes;
} );
Hi Tom, doesn't seem to be working, still outputting the class when the featured image / Page Header is disabled from the page.
Ok, Tom, it's working, had to increase the priority to 20.
Is that a temporary solution, are you planning on fixing that in an upcoming update?
We'll fix this in GPP 1.13.0 :)
Perfect, thanks, Tom!
Hey, Tom. I noticed that the issue hasn't been fixed in GPP 2.0 beta.
Thanks for the heads up - looks like this one slipped through!
Hi Tom. I still don't see that fixed in the RC for GPP 2.0 unless it's a GP free issue. I currently have this code that works only for posts:
add_filter( 'body_class', function( $classes ) {
if ( is_singular() ) {
$disable_featured_image = get_post_meta( get_the_ID(), '_generate-disable-post-image', true );
if ( $disable_featured_image ) {
$classes = array_diff( $classes, array( 'featured-image-active' ) );
}
}
return $classes;
}, 20);
How do I asjust it to affect pages as well?
Yea sorry about that, it didn't make it through on this one. There are a couple of things to take into consideration that I want to think about/get right.
That code should work the same for posts and pages (is_singular() applies to both).
Is the page using the Disable Elements metabox, or a Layout Element?
Disable Elements metabox.
Strange, that should work fine then.
What if you add var_dump('hi'); above the array_diff line? Does it output anything on the site?
Ah, no! It's working fine for pages. There is an issue where the featured image is disabled from the Customizer. What would be the condition to remove the class for them?