WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an image gallery of sorts its displayed on the page using the following template code...

        $featured = get_post_meta($post->ID,'_thumbnail_id',true);
        $attachments = get_children( 'post_type=attachment&orderby=menu_order&exclude='.$featured.'&post_mime_type=image&post_parent='.$post->ID ); ?>

       <ul id="fac">
       <?php foreach ($attachments as $attachment) {
             $img = wp_get_attachment_image_src($attachment->ID);
    $img_large = wp_get_attachment_image_src($attachment->ID, 'full'); ?>
       <li><a rel="example" href="<?php the_permalink() ?>"><img class="fac-img" src="<?php echo $img[0] ?>" width="<?php echo $img[1] ?>" height="<?php echo $img[2] ?>" /></a></li>
       <?php } ?>

The image linking to the main single page of the post.

Now is there any way that one there is only 1 image attached it doesn't show the link?

So you attach one image to the post it shows it on its own.

If however, you upload more than one image it adds the link around that image a long with link text 'click to view more images' or whatever.

Clicking that takes you to a new single.php page where all the images are there running a gallery js script which is already done.

Possible or not?

Thank you for any help.

share|improve this question
up vote 0 down vote accepted

check how many posts were returned in your $attachments variable and output your desired markup for each case:

$featured = get_post_meta($post->ID,'_thumbnail_id',true);
$attachments = get_children( 'post_type=attachment&orderby=menu_order&exclude='.$featured.'&post_mime_type=image&post_parent='.$post->ID );

if( count( $attachments ) > 1 ):
    // more than 1 attachment
    // just 1 (or 0)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.