[resolved] hide a featured image caption (3 posts)

  1. visioncreativewilson
    Posted 2 years ago #

    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?

  2. Michael
    Forum Moderator
    Posted 2 years ago #

    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>'; ?>
  3. visioncreativewilson
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic