Archived topic
Can't display featured image captions
32 replies · Started by Johan on March 30, 2019
Yes, thank you, that was stupid of me... ;-)
No problem :)
I've been looking for this for some time and finally took the plunge on adding a snippet of code using the recommended plugin and a bit of CSS as suggested here. It all works as I hoped.
Glad to hear you found that of use!
This has mostly worked for me, with one problem: The caption is also showing up underneath thumbnail images for the posts in the sidebar. Is there a way to prevent this, so that it only shows up on the main featured image at the top of the post?
Hi Erik,
Try replacing this:
if( $html == '' ) {
with:
if( $html == '' || ! is_single() || ! is_main_query() ) {
Removing caption in single page sidebar didn't work for me after replacing with
if( $html == '' || ! is_single() || ! is_main_query() ) {
Also, the code here, https://gist.github.com/diggeddy/5f7c3a4584a8beb51febc9c4f33d6c5c kind of floats the featured image to the left. I don't want text around the featured image.
UPDATE: RESOLVED! Forgot I added CSS.
I see. Glad you resolved the issue!
THIS PROBLEM has not been resolved
I see. Can you make sure that the quotation marks you used in the code are not slanted? To make sure, can you add the code you added here? Make sure to Highlight the code, and click on "CODE" here in the forum text box so it's added as a code.
To recall, Everything works fine except for the single-post sidebar. See https://scholarsly.com/uniqlo-student-discount/
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 == '' || ! is_single() || ! is_main_query() ) {
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 .= '<div class="wp-caption thumb-caption">';
$out .= $html;
if($thumbnail_image[0]->post_excerpt)
$out .= '<p class="wp-caption-text thumb-caption-text">'.$thumbnail_image[0]->post_excerpt.'</p></div>';
}
return $out;
}
}
Also you'll notice the the sidebar "Latest Post" push to the right and not left. How do I resolve that?
I see. I can see the captions from my end. See: https://share.getcloudapp.com/geuOlqqR
Also, with regards to your second inquiry, can you clarify a bit more about how you would like it to look?
Alternatively, you can also use a GB Query Loop Block to show the Latest Posts. GB Image Blocks has the option to add captions as well.
Example: https://share.getcloudapp.com/KounDwl9
Also see: https://share.getcloudapp.com/4gu4bJOl
Reference: https://docs.generateblocks.com/article/query-loop-overview/
I actually don't want the caption shown.
Well, I've updated the side using GB Query Loop Block (Check the link above). How do I achieve something similar to https://share.getcloudapp.com/P8uNpyro.
And another with numbering & without featured image.