Archived topic
Image Caption and WP Show Posts
14 replies · Started by Alejandro on December 13, 2021
Hi,
I am using WP Show Posts to display related posts. Is working very well but I am having this issue: the image caption is being displayed when I would want to get rid of it (preferably without using display:none in CSS).
To show the caption in the featured image I am using 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 == '' && is_single() ) {
// Get Excerpt of thumbnail
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
$thumbnail_caption = $thumbnail_image[0]->post_excerpt;
if ( $thumbnail_caption ) {
$html .= '<div class="wp-caption thumb-caption">
<p class="wp-caption-text thumb-caption-text">'. $thumbnail_caption .'</p>
</div>';
}
}
return $html;
}
To show Related Posts to every entry in the web I use this code (works well, except the caption...):
<div style="height:30px"></div>
<div class="wpsp-related-posts1 grid-container">
<div class="titulo-modulo-header2"><h3 class="titulo-modulo2">Artículos Interesantes</h3></div><br>
<?php
if ( is_single() ) {
$cats = get_the_category();
$cat = $cats[0];
} else {
$cat = get_category( get_query_var( 'cat' ) );
}
$cat_slug = $cat->slug;
$list = get_page_by_title( 'Posts Relacionados', 'OBJECT', 'wp_show_posts' );
wpsp_display( $list->ID, 'tax_term="' . $cat_slug . '"' );
?>
</div>
I would appreciate any help here. I you want to check what I am talking about see the bottom of THIS post
Thanks! :-)
Hi there,
The caption will show for all loops that use get_the_post_thumbnail() or the_post_thumbnail() function to fetch the post's featured image as thumbnail because you've filtered the post thumbnail itself.
Consider appending the caption through generate_featured_image_output filter instead so it only affects the featured image of the theme.
You can also use generate_inside_featured_image_output to add it in.
Thanks for the answer!
I do not have this technical level :-) sorry. Could you tell me what do I have to replace and where?
(Funny think is that the captions in the featured images of the related posts were not showing until yesterday and suddenly they became to appear up)
I am looking forward to hearing from you. Best,
Alex
Hi,
Any news on my previous question?
Thanks!
A few clarification/s for code write-up:
Do you have any specific conditions for it? Example: caption on single post pages only.
Let us know for PHP code write-up. :D
Hi Elvin,
Yes, captions only should appear on single post pages.
Alex
Hi there,
try 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) {
$class = $attr['class'];
if ( !$html == '' && is_single() && strpos($class, 'wp-post-image') === false ) {
// Get Excerpt of thumbnail
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
$thumbnail_caption = $thumbnail_image[0]->post_excerpt;
if ( $thumbnail_caption ) {
$html .= '<div class="wp-caption thumb-caption">
<p class="wp-caption-text thumb-caption-text">'. $thumbnail_caption .'</p>
</div>';
}
}
return $html;
}
this should check to make sure the image does NOT have the wp-post-imageclass which WPSP images have
Hey David,
Thanks for the answer. I implemented the code but I am afraid that I does not work...
Any extra ideas on how to solve this?
Is my code still added ?
Hi David,
I removed it, but it is again in place... do you need me to remove it again?
Ok instead of filtering lets try simply hooking it in after the featured image:
add_action( 'generate_after_entry_header', 'db_auto_single_featured_caption', 20 );
function db_auto_single_featured_caption() {
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
if ( is_single() && !empty($get_description) ){
echo '<div class="wp-caption">'. $get_description . '</div>';
}
}
Mmmmm, now...
1) I replaced this code by yours:
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) {
$class = $attr['class'];
if ( !$html == '' && is_single() && strpos($class, 'wp-post-image') === false ) {
// Get Excerpt of thumbnail
$thumbnail_image = get_posts(array('p' => $post_thumbnail_id, 'post_type' => 'attachment'));
$thumbnail_caption = $thumbnail_image[0]->post_excerpt;
if ( $thumbnail_caption ) {
$html .= '<div class="wp-caption thumb-caption">
<p class="wp-caption-text thumb-caption-text">'. $thumbnail_caption .'</p>
</div>';
}
}
return $html;
}
2) It kills the image caption in the related posts BUT
3) kills as well the image caption in the featured post :-(
ideas?
Looks like it worked to me:
https://www.screencast.com/t/gEwyHNaNXQ5
Just need to reduce the bottom margin on the featured image. And give the caption some style:
.single.post-image-below-header.post-image-aligned-center .inside-article .featured-image {
margin-bottom: 0;
}
.featured-image + .wp-caption {
font-size: 15px;
}
You were absolutely right David. Thanks for the help!
Glad to be of help!