[Support request] Featured Image Caption Issue

Home Forums Support [Support request] Featured Image Caption Issue

Home Forums Support Featured Image Caption Issue

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1869562
    vanbawilian

    Hi

    I’m using this snippet for single post Featured image caption:

    Unfortunately, image caption texts appear all over my site thumbnail images even in my sidebar thumbnail image. I only want to show the caption text under single post featured image. HELP!

    #1869563
    vanbawilian

    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 .= ‘

    ‘;

    $out .= $html;

    if($thumbnail_image[0]->post_excerpt)
    $out .= ‘<p class=”wp-caption-text thumb-caption-text”>’.$thumbnail_image[0]->post_excerpt.'</p>

    ‘;

    }

    return $out;

    }

    #1869606
    Leo
    Staff
    Customer Support

    Hi 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-information

    Let me know 🙂

    #1869903
    vanbawilian
    #1870072
    David
    Staff
    Customer Support

    In that code – change this line:

    if( $html == '' ) {

    to:

    if( $html == '' || !is_single() ) {

    this will limit to the single post featured image

    #1870127
    vanbawilian

    It is not working. See my site footer, caption texts still appear in thumbnail image.

    Example Here

    #1871172
    David
    Staff
    Customer Support

    Hmm… 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;
    }
    #1871272
    vanbawilian

    I’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.

    #1871436
    David
    Staff
    Customer Support

    Hmmm… 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 );
Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.