- This topic has 11 replies, 3 voices, and was last updated 4 years, 11 months ago by
Tom.
-
AuthorPosts
-
April 15, 2021 at 9:30 pm #1736259
Yuanzao
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!
April 15, 2021 at 9:32 pm #1736260Yuanzao
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() { }April 16, 2021 at 2:08 am #1736466David
StaffCustomer SupportHi there,
this hook to remove it:
add_action( 'after_setup_theme', function() { remove_action( 'generate_after_entry_header', 'generate_post_image' ); } );April 16, 2021 at 4:53 am #1736693Yuanzao
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!
April 16, 2021 at 4:58 am #1736697Yuanzao
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.
April 16, 2021 at 5:47 am #1736746David
StaffCustomer SupportCan you share the code you added/changed so i can understand what its doing.
April 16, 2021 at 6:23 am #1736790Yuanzao
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', '' ) ) ); } } } }April 16, 2021 at 6:44 am #1736815David
StaffCustomer SupportAah ok – now i understand ( having a brain freeze today ).
You can just use the
generate_featured_image_outputfilter in your functions.phpHeres an example of that being used with a conditional:
https://generatepress.com/forums/topic/if-there-is-no-featured-image/#post-1467706
April 16, 2021 at 7:40 am #1737185Yuanzao
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', '' ) ) ); }April 17, 2021 at 8:23 am #1738181Tom
Lead DeveloperLead DeveloperHi 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 );April 17, 2021 at 10:35 pm #1738524Yuanzao
Great, Tom
Thanks a lot!
April 18, 2021 at 8:05 am #1739193Tom
Lead DeveloperLead DeveloperYou’re welcome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.