- This topic has 8 replies, 3 voices, and was last updated 2 years, 8 months ago by David.
-
AuthorPosts
-
July 24, 2021 at 3:54 pm #1869562vanbawilianJuly 24, 2021 at 3:56 pm #1869563vanbawilian
I use this snippet:
add_filter( ‘post_thumbnail_html’, ‘custom_add_post_thumbnail_caption’,10,5 );
function custom_add_post_thumbnail_caption($html, $post_id, $post_thumbnail_id, $size, $attr) {
if( $html == ” ) {
return $html;
} else {
$out = ”;
$thumbnail_image = get_posts(array(‘p’ => $post_thumbnail_id, ‘post_type’ => ‘attachment’));
if ($thumbnail_image && isset($thumbnail_image[0])) {
$image = wp_get_attachment_image_src($post_thumbnail_id, $size);
if($thumbnail_image[0]->post_excerpt)
$out .= ‘‘;
}
return $out;
}
July 24, 2021 at 5:04 pm #1869606LeoStaffCustomer SupportHi there,
Any chance you can link us to the site in question?
You can use the private information field:
https://docs.generatepress.com/article/using-the-premium-support-forum/#private-informationLet me know 🙂
July 25, 2021 at 3:07 am #1869903vanbawilianJuly 25, 2021 at 6:04 am #1870072DavidStaffCustomer SupportIn that code – change this line:
if( $html == '' ) {
to:
if( $html == '' || !is_single() ) {
this will limit to the single post featured image
July 25, 2021 at 7:10 am #1870127vanbawilianIt is not working. See my site footer, caption texts still appear in thumbnail image.
Example Here
July 26, 2021 at 4:15 am #1871172DavidStaffCustomer SupportHmm… try this code instead:
add_action( 'generate_after_entry_header', 'db_auto_single_featured_caption', 25 ); function db_auto_single_featured_caption( $html ) { $caption = get_the_post_thumbnail_caption(); if ( is_single() && !empty($caption) ){ $html .= '<div class="wp-caption">'. $caption . '</div>'; } return $html; }
July 26, 2021 at 5:33 am #1871272vanbawilianI’ve already tried this snippet before. But the problem is the caption appears before featured image taking a wide space between featured image and post title.
July 26, 2021 at 7:47 am #1871436DavidStaffCustomer SupportHmmm… give this a shot:
add_filter( 'generate_single_featured_image_output', function( $output, $image_html ) { $caption = get_the_post_thumbnail_caption(); if ($caption) { $caption_html = '<div class="wp-caption">'. $caption . '</div>'; } printf( '<div class="featured-image page-header-image-single"> %1$s <div class="wp-caption">%2$s</div> </div>', $image_html, $caption_html, ); }, 10, 2 );
-
AuthorPosts
- You must be logged in to reply to this topic.