Site logo

Archived topic

Caption for post thumbnails

32 replies · Started by Michael Jaegers on March 13, 2015

Viewing posts 1–15 of 33

Hi,

for legal purposes I would like to have the caption of the post thumbnail on the main page visible.

Is this possible?

Best regards,

Michael

Hi there,

Unfortunately the default WordPress Featured Image only grabs an image - no caption.

Options:

a) Add the caption to the image itself that you're uploading as the post thumbnail.
b) Try adding the image with the caption code (from the post) into the "Page Header" content area. Then check the "Add to blog excerpt" checkbox.

Let me know :)

Hi Tom,

unfortunately both options do not work.

I saw some code snippets which should do what I need, but I cannot find, where to place them>

<?php echo get_post(get_post_thumbnail_id())->post_post; ?>

Any other idea,

Michael

Hmm, the above code definitely wouldn't do it.

You may be able to remove the current post thumbnail with a remove_action call, and then insert your own post thumbnail function in with included code for the caption using a plugin like this: https://wordpress.org/plugins/featured-image-caption/

However, it would involve a decent amount of PHP to do so.

I can look into it further if you like, but I think a or b would still work, and may be better with less hacking into the core theme.

Ok, I gave it another try with both options.

I added two pictures and added a title, description and so on to both of them. Now I created two posts (Option A and Option B). For Bption A i added one picture with some fancy guys as post thumbnail. For option B i added the other picture in the Post Header Image field.

Have a look at Jaegers.Net and scroll to January 1st to see the results. Both pictures are visible but no kind of picture description or caption, only for them, which are addid directly to the post content.

Best regards,

Michael

Hi Michael,

Using Option B (using the page header metabox), why not do something like this?:

<div class="wp-caption alignnone"><img src="http://jaegers.net/wp-content/uploads/2015/03/IMG_0003.jpg" alt=""><p class="wp-caption-text">Your caption here</p></div>

You would add the above to the "Content" field, and then update the image URL and caption.

Hmm, it's working, but it's also an ugly solution as there are many manual actions required per post.

Fair enough, another option is to use a plugin like this: https://wordpress.org/plugins/featured-image-caption/

Then remove the post image function:

add_action('after_setup_theme','generate_remove_post_image');
function generate_remove_post_image()
{
      remove_action( 'generate_after_entry_header', 'generate_post_image' );
}

Then re-add your new function with the caption code:

add_action( 'generate_after_entry_header', 'generate_post_image_caption' );
function generate_post_image_caption()
{
		
	// If there's no featured image, return
	if ( ! has_post_thumbnail() )
		return;
		
	// If we're not on any single post/page or the 404 template, we must be showing excerpts
	if ( ! is_singular() && ! is_404() ) {
	?>
		<div class="post-image">
			<a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'full', array('itemprop' => 'image') ); ?><?php if ( function_exists( 'cc_featured_image_caption' ) ) : cc_featured_image_caption(); endif; ?></a>
		</div>
	<?php
	}
}

Hi Tom,

thank you for your support. Sorry, I didn't want to annoy you. I really appreciate your excellent and quick support! For me it's ok so far.

Best regards,

Michael

No problem! The more options the better! :)

Hi.

I'm having an issue with not being able to set a caption for my featured image as well. I downloaded and installed the plugin you mentioned but I don't see the code you're referring to in the plugin code. Am I looking in the right place?

Thanks!
:) Emily

Hi, Tom.

So I have added the plugin you suggested and downloaded the child theme and am using it. I put your code in the functions.php file. You can see the result here: cafeemily.com

The post thumbnails are being duplicated as well as the thumbnail captions on the duplicate image.

What I want is to be able to display the image caption for the thumbnails AND have them display on the large post image in the post itself. Right now, the caption is only showing up for the thumbnails which the plugin was doing before I added your code.

I can obviously remove the code you gave me and get rid of the duplicate problem but I would really like to display the captions on the actual image inside the post itself.

I really appreciate your help. You don't have to do this but it makes my life a lot easier. I have coding experience but I'm not nearly as knowledgeable about the WordPress framework as you are.

Thanks!
:) Emily

Looks like you're using the blog add-on, which requires a slightly different function to remove the initial one:

add_action('after_setup_theme','generate_remove_blog_post_image');
function generate_remove_blog_post_image()
{
      remove_action( 'generate_after_entry_header', 'generate_blog_post_image' );
}

Hopefully that does it for you :)

Hi, Tom.

Not sure what happened but I got a parse error on line 9 when I replaced the old remove function with the new one. This is what my functions.php looks like. Maybe you can tell me what the problem is?

<?php
/**
 * Generate child theme functions and definitions
 *
 * @package Generate
 */
add_action('after_setup_theme','generate_remove_blog_post_image');

function generate_remove_blog_post_image()

{

      remove_action( 'generate_after_entry_header', 'generate_blog_post_image' );

}

add_action( 'generate_after_entry_header', 'generate_post_image_caption' );
function generate_post_image_caption()
{
		
	// If there's no featured image, return
	if ( ! has_post_thumbnail() )
		return;
		
	// If we're not on any single post/page or the 404 template, we must be showing excerpts
	if ( ! is_singular() && ! is_404() ) {
	?>
		<div class="post-image">
			<a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_post_thumbnail( 'full', array('itemprop' => 'image') ); ?><?php if ( function_exists( 'cc_featured_image_caption' ) ) : cc_featured_image_caption(); endif; ?></a>
		</div>
	<?php
	}
}

Thanks!
:) Emily

This archived topic is closed to new replies.