[Resolved] Add featured image caption via Elements

Home Forums Support [Resolved] Add featured image caption via Elements

Home Forums Support Add featured image caption via Elements

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1755394
    JC

    To display credits I need to show image caption below the image on post pages.

    If you can help point me in the right direction and possibly that I can do it via elements. The 2 solutions on the forum does not include how to via elements and also the one that did work added it after the post title and not the image so I must have used the wrong hook for it.

    Thank you in advance ๐Ÿ™‚

    #1755776
    David
    Staff
    Customer Support

    Hi there,

    you would need to create a shortcode to display the caption, which can then be added using Elements – heres an example which also includes a fallback text if there is no image caption set:

    https://generatepress.com/forums/topic/show-specific-text-if-fallback-image-is-used-instead-featured-image/#post-1686941

    #1756193
    JC

    Hi David,

    Thank you for the quick response.

    Ok, so if I do a shortcode might as well add it to the functions.php only so how do I get it to display after the featured image (below it) by adding it to the functions.php?

    #1756567
    Elvin
    Staff
    Customer Support

    Hi there,

    You can use this filter to modify the featured image to add your caption.
    https://docs.generatepress.com/article/generate_featured_image_output/

    You can either insert a do_shortcode() in it containing your created shortcode or code something within the filter.

    #1756892
    JC

    Hi Elvin,

    It’s safe to assume that I am not proficient when it comes to PHP but of all the solutions this one ended up working for me;

    https://generatepress.com/forums/topic/featured-image-badge/

    #1756898
    Elvin
    Staff
    Customer Support

    Have you sorted this out? The link you’ve added points to a search results list.

    Let us know if you need further help. ๐Ÿ™‚

    #1756914
    JC

    I’ve updated the link to the correct one, https://generatepress.com/forums/topic/featured-image-badge/

    The only problem is that displays the description and not the caption so how do I modify the below to display the caption and not the description from the image?

    add_action( 'post_thumbnail_html', 'db_auto_single_featured_description', 10 );
    function db_auto_single_featured_description( $html ) {
        $get_description = get_post(get_post_thumbnail_id())->post_excerpt;
        if ( is_single() && !empty($get_description) ){
          $html .= '<div class="wp-caption">'. $get_description . '</div>';
        }
        return $html;
    }
    #1757071
    David
    Staff
    Customer Support

    Using this snippet for the Caption:

    add_action( 'post_thumbnail_html', 'db_auto_single_featured_caption', 10 );
    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;
    }

    And a little bit off CSS to style it:

    .featured-image .wp-caption {
        line-height: 1.5em;
        font-size: 12px;
        text-align: left;
    }
    #1757458
    JC

    Thank you David! You are a superstar as always!

    #1758384
    David
    Staff
    Customer Support

    Glad to be of help

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.