Archived topic
can't cover generate_post_image via function.php
11 replies · Started by Yuanzao on April 15, 2021
Hi,There
I want to cover the generate_post_image function from /inc/structure/featured-images.php in child theme function.php
because I want to use custom field image url to be my future images.
Please help, Thanks!
I tried this code but it didn't work
add_action( 'wp','generate_remove_post_image', 20 );
function generate_remove_post_image() {
remove_action( 'generate_after_entry_header', 'generate_post_image' );
}
add_action( 'generate_after_entry_header', 'hoh_generate_blog_post_image' );
function hoh_generate_blog_post_image() {
}
Hi there,
this hook to remove it:
add_action( 'after_setup_theme', function() {
remove_action( 'generate_after_entry_header', 'generate_post_image' );
} );
Hi, David
Thanks for the help.
I tried the code, it doesn't work.
Here's what I did in customize theme
What I expected is writing a new function in function.php to override the generate_post_image function from featured-images.php
So I can use the customize theme setting but custom field url image.
Thanks!
Right now I made change on generate_post_image function in featured-images.php
it works, but I would like to move the code to child theme function.php so it won't bother me for updating.
Can you share the code you added/changed so i can understand what its doing.
Hi, David
Here's what I modified in featured-images.php (in /inc/structure/ folder)
I would like to move the changed code to function.php so it won't bother me for updating.
if ( ! function_exists( 'generate_post_image' ) ) {
add_action( 'generate_after_entry_header', 'generate_post_image' );
/**
* Prints the Post Image to post excerpts
*/
function generate_post_image() {
// If there's no featured image, return.
$custom_field_image_url = get_post_meta(get_the_ID(), 'cover_image_url', TRUE);
if ( ! has_post_thumbnail() AND ! $custom_field_image_url) {
return;
}
// If we're not on any single post/page or the 404 template, we must be showing excerpts.
if ( ! is_singular() && ! is_404() ) {
$attrs = array();
if ( 'microdata' === generate_get_schema_type() ) {
$attrs = array(
'itemprop' => 'image',
);
}
if( has_post_thumbnail() )
{
echo apply_filters( // phpcs:ignore
'generate_featured_image_output',
sprintf(
'<div class="post-image">
%3$s
<a href="%1$s">
%2$s
</a>
</div>',
esc_url( get_permalink() ),
get_the_post_thumbnail(
get_the_ID(),
apply_filters( 'generate_page_header_default_size', 'full' ),
$attrs
),
apply_filters( 'generate_inside_featured_image_output', '' )
)
);
}
if($custom_field_image_url and !has_post_thumbnail())
{
echo apply_filters( // phpcs:ignore
'generate_featured_image_output',
sprintf(
'<div class="post-image">
%3$s
<a href="%1$s">
<img src="%2$s" loading="lazy" itemprop="image" width="300" height="204" />
</a>
</div>',
esc_url( get_permalink() ),
$custom_field_image_url,
apply_filters( 'generate_inside_featured_image_output', '' )
)
);
}
}
}
}
Aah ok - now i understand ( having a brain freeze today ).
You can just use the generate_featured_image_output filter in your functions.php
Heres an example of that being used with a conditional:
https://generatepress.com/forums/topic/if-there-is-no-featured-image/#post-1467706
Hi, David
Thanks for the notice.
Actually I tried this filter "generate_featured_image_output"
However you can see it only output when the post has thumbnail.
I am using custom field url for replacing the thumbnail, so it won't even output the html because it's under "has_post_thumbnail"
if( has_post_thumbnail() )
{
echo apply_filters( // phpcs:ignore
'generate_featured_image_output',
sprintf(
'<div class="post-image">
%3$s
<a href="%1$s">
%2$s
</a>
</div>',
esc_url( get_permalink() ),
get_the_post_thumbnail(
get_the_ID(),
apply_filters( 'generate_page_header_default_size', 'full' ),
$attrs
),
apply_filters( 'generate_inside_featured_image_output', '' )
)
);
}
Hi there,
has_post_thumbnail() has a filter itself, so we can tell WP you have a post thumbnail if the custom field exists: https://developer.wordpress.org/reference/functions/has_post_thumbnail/
add_filter( 'has_post_thumbnail', function( $has, $post ) {
$custom_field_image_url = get_post_meta( $post, 'cover_image_url', TRUE);
if ( $custom_field_image_url ) {
$has = true;
}
return $has;
}, 10, 2 );
Great, Tom
Thanks a lot!
You're welcome :)