WordPress.org

Forums

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

  1. visioncreativewilson
    Member
    Posted 1 year 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>'; ?>
            </div>
    	</div>
            <div>
            <?php the_content();?>
            </div>
     <?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. alchymyth
    Forum Moderator
    Posted 1 year 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
    Member
    Posted 1 year 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