Ask your WordPress questions! Pay money and get answers fast! (more info)

display only if the post has more than 1 image attached to it

  • SOLVED

I have a website where I have the featured image displayed above the content as a show piece image. I also have the gallery code <?php echo do_shortcode('[gallery link="file"]'); ?> below the content to automatically display if ALL the images attached to my post.

Now, if I only attach 1 image it will be the featured image above the content and therefore no need to show the gallery which would be the same featured image below the content. If I attached 2 images however the gallery would look much more appropriate.

Looking for a piece of code that would allow this.

if attached images > 1
then do "this"
elseif attached images =<1
then do "something else"

Answers (1)

2014-07-09

Arnav Joy answers:

try this

<?php

$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID()
);


$images = get_posts( $args );

if( count( $images ) == 1 ){
// Do here
}
else if( count( $images ) > 1 ){
// Do here
}
else {
// do here
}


?>


pjeaje comments:

Thanks, before i test it can you modify it so that the php tags are closed ?> and reopened <?php so i can fit my // do here as html


Arnav Joy comments:

here it is

<?php

$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID()
);


$images = get_posts( $args );

if( count( $images ) == 1 ){
?>
HTML Goes HERE
<?php
}
else if( count( $images ) > 1 ){
?>
HTML Goes HERE
<?php
}
else {
?>
HTML Goes HERE
<?php
}


?>


pjeaje comments:

Thanks, works great.