• Resolved dfwgreg

    (@dfwgreg)


    Hi everyone,
    I’m looking for a way to check if a post has an attachment; display the thumbnail if it does and another image if it doesn’t. I’ve tried the is_attachment to no avail.

    This is what I’ve done:
    <?php if (is_attachment()) { ?><?php $attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') ); foreach ( $attachments as $attachment_id => $attachment ) { echo wp_get_attachment_image( $attachment_id, 'thumbnail' );} ?><?php } else { ?>another image<?php } ?>

    What the above code does is print another image, even if a post has an attachment.

    All help is greatly appreciated and this will be my final question.

    Thanks,
    Gregory S.

Viewing 1 replies (of 1 total)
  • Thread Starter dfwgreg

    (@dfwgreg)

    Solved:

    <?php
    $attachments = get_children(
        array(
    	'post_type' => 'attachment',
    	'post_mime_type' => 'image',
    	'post_parent' => $post->ID
        ));
    if(count($attachments) > 0) { ?>
        <?php
    		$attachments = get_children(array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order'));
    		if ( ! is_array($attachments) ) continue;
    		$count = count($attachments);
    		$first_attachment = array_shift($attachments);
    		?>
    		<?php echo wp_get_attachment_image($first_attachment->ID, 'thumbnail'); ?>
    <?php } else { ?>
        <img src="/img/alttv_small.jpg">
    <?php } ?>

Viewing 1 replies (of 1 total)
  • The topic ‘How to check for attachment and display a different image if not’ is closed to new replies.