WordPress.org

Forums

[resolved] How to check if image title, caption, description exist befor showing it? (3 posts)

  1. Jole
    Member
    Posted 1 year ago #

    Hello.

    I use this code to display image title, caption (excerpt), description (content):

    echo get_post(get_post_thumbnail_id())->post_title;
    echo get_post(get_post_thumbnail_id())->post_excerpt;
    echo get_post(get_post_thumbnail_id())->post_content;

    But, how to check if there is any of those fields first. For example, how to check if there is caption, and if not to show some other text?

    Best regards.

  2. karpstrucking
    Member
    Posted 1 year ago #

    Hello,

    You can set a variable in an if statement:

    if ( $title = get_post( ... ) ) {
      echo $title;
    }

    Thanks,
    Lucas

  3. Jole
    Member
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic