How to use and style the_post_thumbnail_caption?
-
I have a blog that uses a custom child theme for Twenty Fifteen, with a couple modifications I added myself. Unfortunately, one of those was the featured image caption that caused a lot of crashes post 4.6, as I discovered after a search through these forums.
Here’s what I used to have in functions.php:
// Ensure featured image captions // function the_post_thumbnail_caption() { global $post; $thumbnail_id = get_post_thumbnail_id($post->ID); $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment')); if ($thumbnail_image && isset($thumbnail_image[0])) { $caption = $thumbnail_image[0]->post_excerpt; } return $caption; }
And here is how it would work in content-single.php:
<?php twentyfifteen_post_thumbnail(); if(has_post_thumbnail()) { $caption = the_post_thumbnail_caption(); if($caption) echo '<div class="featuredcap">' . "\n"; if($caption) { echo '<p class="featured-caption-text">' . $caption . '</p>' . "\n"; echo '</div>' . "\n"; } } ?>
The featuredcap and featured-caption-text would then have corresponding styles in my child theme style.css
Now, even after identifying what caused the clash after the latest update, I am no longer able to get the captions to work! At best, I can have them appear on the site, but no styling is applied.
I have tried re-naming the original scavenged function, which does still display the captions, but for some weird reason the CSS styles would not apply any more.
Ultimately, because the_post_thumbnail_caption now exists, I thought it would be better to just use it in content-single.php and style it accordingly. However, my php knowledge is rudimentary at best, so I haven’t been able to achieve this.
Even if I use:
<?php twentyfifteen_post_thumbnail(); ?> <div class="wp-caption"><?php the_post_thumbnail_caption(); ?></div>
the image caption is rendered, but no CSS styles are applied to it, not even the standard ones for image captions elsewhere on the site.
Tl;dr: I can get Featured Image captions to show, but how do I style them correctly?
The site is nevertoocurious.com/blog
Any help would be much appreciated!
- The topic ‘How to use and style the_post_thumbnail_caption?’ is closed to new replies.