Site logo

Archived topic

Can't display featured image captions

32 replies · Started by Johan on March 30, 2019

Viewing posts 1–15 of 33

Hello.
I want image captions for featured images in posts to show, so I can give attribution and copyright. But no matter what I do, it does not seem to be possible.

With Simple CSS I try to ad this:

.post-image-above-header .inside-article .featured-image {
margin-bottom: 0 !important;
}
.featured_caption {
font-size: 75%;
padding-top: 5px;
opacity: .8;
margin-bottom: 1.5em;
}`

I also try to add some different solutions from these here forums. Like this:

add_action( 'generate_after_entry_header', 'db_featured_image_caption', 15 );
function db_featured_image_caption() {
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
if(!empty($get_description)){
echo '<span class="featured_caption">' . $get_description . '</span>';
}
}`

But with no luck.

I also try to add things like this:

.featured_caption {
    text-align; center;
}

Still no luck.

Not even with this:

.page-header-image-single {
text-align: center;
}
.featured_caption {
text-align; center;
}

Furthermore I try to add some PHP snippets:


add_action( 'generate_before_content', 'db_featured_image_caption', 15 );
function db_featured_image_caption() {
    $get_description = get_post(get_post_thumbnail_id())->post_excerpt;
    if(!empty($get_description) && is_single() ){    
        echo '<span class="featured_caption">' . $get_description . '</span>';
    }
}

No luck with that either.

What can I do to get captions under the featured images in my posts?

Thanks. It kinda works, but the letters of the caption floats up into the pictures. Is there any way to adjust the caption text a little down, and to the left, so that it looks just like the normal captions that show up under any other images in a post?

Furthermore it would be preferable to have more than one line of text for the caption under the featured image in posts. In case the attributions are longer than one line.

You can give it some custom styling like so:

.featured-image .wp-caption-text {
    padding: 10px;
    max-width: 770px;
    margin: auto;
    line-height: 1.4em;
}

Thanks. It now works.

Glad to be of help

Hello David,
just used this snippet for a while and it works fine. I wonder if there's a way to add this filter only on a single post?
Best, Daniel

Hi there,

you can try swapping this line:

if( $html == '' ) {

for

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

So it returns the default HTML if there is no featured image or is !NOT a single post

David, you're today my personal hero. That works! Thanks a lot!

You're welcome :)

Hello David! Thanks for the information it helped me a lot.

We are using Shortcode Ultimate (Unfortunately) to feature some other posts within the article. It shots the title, an exerpt and the image. So now I see the caption not only for the image on the original post itself but also on other pages when the featured images is used.

example

Hope you can help me with that and I promise this is my last question for a while ;-)

Thank you!

Hi there,

would you mind raising a new topic where you can share a link so i can take a look. The most i reckon will be some CSS to hide it.

Hi David,
I also would like to show the caption text below a featured image on a post.
I've added your php code as a snippet, and added the css code to the customizer.
but I don't see the text back on the front end.

An example is https://yolandalooptdoor.yory.nl/mijn-interview-in-het-blad-radar/

Can you help out?

Thank you,
Bye, Yolanda

Did you clear the caching plugin after adding the code?

This archived topic is closed to new replies.