Archived topic
Removing featured image action does not work
5 replies · Started by Jeffrey on May 5, 2021
Hi there!
I created a child theme of GeneratePress and trying to do the following.
1. Remove featured image on a single post
2. Add my own featured image to the single post
Adding my own featured image does work quite well.
add_action( 'generate_after_entry_header', function() {
if ( ! is_single() || ! has_post_thumbnail() ) {
return;
}
$attrs = array();
if ( 'microdata' === generate_get_schema_type() ) {
$attrs = array(
'itemprop' => 'image',
);
}
?>
<div class="featured-image youtube-link grid-container grid-parent">
<?php
the_post_thumbnail(
apply_filters( 'generate_page_header_default_size', 'full' ),
$attrs
);
?>
</div>
<?php
} );
But removing the GeneratePress featured image does not seem to work. I checked with var_dump() if the remove_action() functions returns true, and it does. Yet when I load my page see two featured images. First one is my custom image, second is the GeneratePress image.
add_action( 'wp', function() {
if ( is_single() ) {
var_dump( remove_action( 'generate_after_entry_header', 'generate_post_image' ) );
var_dump( remove_action( 'generate_after_header', 'generate_featured_page_header' ) );
var_dump( remove_action( 'generate_before_content', 'generate_featured_page_header_inside_single' ) );
}
} );
This outputs the following at the top of the page:
bool(true) bool(true) bool(true)
Hope you can help me out.
Hi there,
Have you tried removing the default featured image through the customizer settings?
Try unchecking "display featured image" on post tab.
https://docs.generatepress.com/article/adjusting-the-featured-images/
You may also want to consider filtering the default featured image output to your preference instead of adding a new one and removing the default.
https://docs.generatepress.com/article/generate_featured_image_output/
Hi Elvin,
Thank you for your reply. I did check the settings at the customizer tab. If I incheck it, it does not show any image -- as expected. If I check it, it shows the two featured images at the single post because of my script.
Now, I did try to use the filter first. I didn't want to use the add_action method. But when I use the filter you redirected me to, it does not change the featured image at the single post. It only changes the featured image at the archive list, which seems a bit odd to me.
add_filter( 'generate_featured_image_output', function( $output ) {
return 'FILTERED';
} );
Also, I did not add any specific pages to my child time like single.php or archive.php. It just contains
- admin/
- functions.php
- style.css
- screenshot.png
I got this from your forum of one of your colleagues who made a starterpack for using a child theme with GeneratePress. I'd love to use a filter to change the featured image of my single post, if possible.
Thank you for your help!
Hi there,
Have you tried the generate_single_featured_image_output filter?
add_filter( 'generate_single_featured_image_output', function( $output, $image_html ) {
return sprintf(
'<div class="your-custom-classes">%s</div>',
$image_html
);
}, 10, 2 );
Using the Customizer option to disable the default featured image should work as well - it's not aware of your custom function adding another image in, so it shouldn't be able to prevent it from working.
Tom, this works just great. I was looking for this filter but I guess I wasn't looking good enough in the docs. Thank you for your time!
Glad I could help! :)