2

I'd like to wrap my post thumbnail with <figure> tags.

I have tried the following and the image appears, but the <figure> tags come up empty.

<?php
if ( has_post_thumbnail() ) {
    echo '<figure>'.the_post_thumbnail('gallery').'</figure>';
}
?>

I've also tried the following, but the image is missing and only the empty <figure> tags appear.

<?php
if ( has_post_thumbnail() ) {
    echo '<figure>'.get_the_post_thumbnail('gallery').'</figure>';
}
?>

UPDATE

For anyone wanting the full code from the answer, here it is:

<?php
if ( has_post_thumbnail() ) {
    echo '<figure>'.get_the_post_thumbnail( $page->ID, 'gallery').'</figure>';
}
?>
0
2

Use get_the_post_thumbnail( $id, $size ) if you're going to echo the result.

And make sure you have post thumbs enabled.

add_theme_support( 'post-thumbnails' );
2
  • This didn't work unfortunately. I've updated my question to reflect your suggestion. – Sam Jan 6 '16 at 15:00
  • You're missing the post/post id. Read the codex – jgraup Jan 6 '16 at 15:01
0

The feature became available with Version 2.9. Note that you can optionally pass a second argument with an array of the Post Types for which you want to enable this feature.

add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails', array( 'post' ) );          // Posts only
add_theme_support( 'post-thumbnails', array( 'page' ) );          // Pages only
add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); // Posts and Movies

This feature must be called before the init hook is fired. That means it needs to be placed directly into functions.php or within a function attached to the 'after_setup_theme' hook.

NOTE: For custom post types, you can also add post thumbnails using the register_post_type function as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.