[Resolved] Image Caption and WP Show Posts

Home Forums Support [Resolved] Image Caption and WP Show Posts

Home Forums Support Image Caption and WP Show Posts

  • This topic has 14 replies, 3 voices, and was last updated 1 month ago by David.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #2047793
    Alejandro

    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! 🙂

    #2048115
    Elvin
    Staff
    Customer Support

    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.

    A wise man once said:
    "Have you cleared your cache?"

    #2048372
    Alejandro

    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

    #2049298
    Alejandro

    Hi,
    Any news on my previous question?

    Thanks!

    #2049361
    Elvin
    Staff
    Customer Support

    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. 😀

    A wise man once said:
    "Have you cleared your cache?"

    #2049763
    Alejandro

    Hi Elvin,
    Yes, captions only should appear on single post pages.

    Alex

    #2049821
    David
    Staff
    Customer Support

    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

    #2049872
    Alejandro

    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?

    #2049922
    David
    Staff
    Customer Support

    Is my code still added ?

    #2050391
    Alejandro

    Hi David,
    I removed it, but it is again in place… do you need me to remove it again?

    #2051186
    David
    Staff
    Customer Support

    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>';
        }
    }
    #2051407
    Alejandro

    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?

    #2052000
    David
    Staff
    Customer Support

    Looks like it worked to me:

    2021-12-17_14-45-07

    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;
    }
    #2055485
    Alejandro

    You were absolutely right David. Thanks for the help!

    #2055498
    David
    Staff
    Customer Support
Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.