Support » Fixing WordPress » hide a featured image caption

  • Resolved visioncreativewilson


    The following is what I use to check for a featured image with caption:

    <?php // Checking for a post thumbnail
            if ( has_post_thumbnail() ) { ?>
            <div style="float:left; width:auto; padding-right:5px;">
    	<div style="float:left; position:relative;">
            <?php the_post_thumbnail("news-large", array('class' => 'enewspost')); echo '<div style="width:auto;"><p class="wp-caption-thumbnail">'.get_post(get_post_thumbnail_id())->post_excerpt. '</p></div>'; ?>
            <?php the_content();?>
     <?php }
    	else { ?>

    the <p class wp-caption> has styling elements like grey background, my question is:

    If the caption is empty, how can I get the <p class> to hide?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael


    Forum Moderator

    try to use a ternary operator to check the existence of the caption (example – untested):

    <?php the_post_thumbnail("news-large", array('class' => 'enewspost')); echo '<div style="width:auto;">' . ( get_post(get_post_thumbnail_id())->post_excerpt ? '<p class="wp-caption-thumbnail">'.get_post(get_post_thumbnail_id())->post_excerpt. '</p>' : '' ). '</div>'; ?>

    Success! Thank you very much!

    Could you possibly explain that solution? I’m not much of a php programmer, and after some research, I thought I would have to incorporate something like this:

    if (!empty($thumbnail_image[0]->post_excerpt))

    Thanks again

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘hide a featured image caption’ is closed to new replies.