Forums

Conditional Tag for Attachment (3 posts)

  1. viitoria
    Member
    Posted 9 months ago #

    Is there such a conditional tag to define if a post has an attachment (especially an image) or not? For example, if there were an image attached to the post, then I would call up the thumbnail of the image, but otherwise it would show the post avatar?

    Thanks.

  2. viitoria
    Member
    Posted 9 months ago #

    anyone?

  3. MichaelH
    moderator
    Posted 9 months ago #

    With this search:
    http://www.google.com/search?q=wordpress+how+see+if+post+has+thumbnail+image

    found this forum thread:
    http://wordpress.org/support/topic/195811

    So used that for this.

    <?php
    $images =get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
    if ($images) {
    foreach( $images as $imageID => $imagePost ) {
    $thumb_url = wp_get_attachment_thumb_url($imageID);
    if ($thumb_url ) {
    echo 'here is the thumbfile url ' . $thumb_url;
    //**just in case echo wp_get_attachment_image($imageID, 'thumbnail', false);
    }
    }
    }
    ?>

Reply

You must log in to post.

About this Topic