- This topic has 14 replies, 3 voices, and was last updated 2 years, 4 months ago by David.
-
AuthorPosts
-
December 13, 2021 at 10:09 am #2047793Alejandro
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! 🙂
December 13, 2021 at 5:16 pm #2048115ElvinStaffCustomer SupportHi there,
The caption will show for all loops that use
get_the_post_thumbnail()
orthe_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.December 14, 2021 at 1:38 am #2048372AlejandroThanks 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
December 14, 2021 at 3:51 pm #2049298AlejandroHi,
Any news on my previous question?Thanks!
December 14, 2021 at 6:58 pm #2049361ElvinStaffCustomer SupportA 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. 😀
December 15, 2021 at 5:03 am #2049763AlejandroHi Elvin,
Yes, captions only should appear on single post pages.Alex
December 15, 2021 at 5:54 am #2049821DavidStaffCustomer SupportHi 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-image
class which WPSP images haveDecember 15, 2021 at 6:55 am #2049872AlejandroHey 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?December 15, 2021 at 7:42 am #2049922DavidStaffCustomer SupportIs my code still added ?
December 15, 2021 at 1:24 pm #2050391AlejandroHi David,
I removed it, but it is again in place… do you need me to remove it again?December 16, 2021 at 8:27 am #2051186DavidStaffCustomer SupportOk 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>'; } }
December 16, 2021 at 2:23 pm #2051407AlejandroMmmmm, 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?
December 17, 2021 at 7:47 am #2052000DavidStaffCustomer SupportDecember 21, 2021 at 6:05 am #2055485AlejandroYou were absolutely right David. Thanks for the help!
December 21, 2021 at 6:11 am #2055498DavidStaffCustomer SupportGlad to be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.