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

Styling gallery depending on how many images WordPress

  • SOLVED

Hi, I would like to show all images that are uploaded into the 'pages' gallery via the page's media uploader, excluding the featured image.

However I would like to style these depending on how many images have been uploaded, for example:

if one image display thumb (link to file) at size: H: 175px by W: 555px (cropped to size) With columns="1"
if two images display thumb (link to file) at size: H: 175px by W: 270px (cropped to size) With columns="2"
if three images display thumb (link to file) at size: H: 175px by W: 172px (cropped to size) With columns="3"
if four or more images display thumb (link to file) at size: H: 127px by W: 127px (cropped to size) With columns="4"

Here is the code that we are using so far.

<?php $idt = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
<?php $images =& get_children( 'post_parent='.$id.'&post_type=attachment&post_mime_type=image' );
if( count($images) > 0 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" size="medium" columns="1"]');
if( count($images) > 1 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" columns="2"]');
if( count($images) > 2 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" columns="3"]');
if( count($images) > 3 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" columns="4"]'); ?>

Please let us know if you are able to help with this.

Thanks

Answers (2)

2013-04-05

Arnav Joy answers:

TRY THIS

<?php $idt = get_post_thumbnail_id(get_the_ID());
$args = array(
'post_type' => 'attachment',
'numberposts' => -1 ,
'exclude' => $idt,
'post_mime_type' => 'image',
'post_status' => NULL ,
'post_parent' => get_the_ID()
);

$images = get_posts( $args );


if( count($images) == 1 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" size="medium" columns="1"]');
if( count($images) == 2 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" columns="2"]');
if( count($images) == 3 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" columns="3"]');
if( count($images) >= 4 ) echo do_shortcode('[gallery exclude='.$idt.' option1="value1" link="file" columns="4"]'); ?>


Arnav Joy comments:

try this

<?php
$idt = get_post_thumbnail_id(get_the_ID());
$args = array(
'post_type' => 'attachment',
'numberposts' => -1 ,
'exclude' => $idt,
'post_mime_type' => 'image',
'post_status' => NULL ,
'post_parent' => get_the_ID()
);

$images = get_posts( $args );


if( count($images) == 1 ) {
if ($images) {
echo '<ul>' ;
foreach ( $images as $image ) {
echo '<li><a href="'.get_permalink( $image->ID ).'"><img src="'.wp_get_attachment_image_src( $image->ID, array( 555,175 ) );.'" /></a><li>';
}
echo '</ul>';
}
}

if( count($images) == 2 ) {
if ($images) {
echo '<ul>' ;
foreach ( $images as $image ) {
echo '<li><a href="'.get_permalink( $image->ID ).'"><img src="'.wp_get_attachment_image_src( $image->ID, array( 270,175 ) );.'" /></a><li>';
}
echo '</ul>';
}
}

if( count($images) == 3 ) {
if ($images) {
echo '<ul>' ;
foreach ( $images as $image ) {
echo '<li><a href="'.get_permalink( $image->ID ).'"><img src="'.wp_get_attachment_image_src( $image->ID, array( 172,175 ) );.'" /></a><li>';
}
echo '</ul>';
}
}

if( count($images) >= 4 ) {
if ($images) {
echo '<ul>' ;
foreach ( $images as $image ) {
echo '<li><a href="'.get_permalink( $image->ID ).'"><img src="'.wp_get_attachment_image_src( $image->ID, array( 127,127 ) );.'" /></a><li>';
}
echo '</ul>';
}
}



?>

2013-04-05

Daniel Yoen answers:

You can try this :

add custom image size in your functions.php :

add_image_size( 'one-column', 555, 175, true );
add_image_size( 'two-column', 270, 175, true );
add_image_size( 'three-column', 172, 175, true );
add_image_size( 'four-column', 127, 127, true );


then :

$thumb_id = get_post_thumbnail_id(get_the_ID());
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_parent' => $thumb_id
);
$attachments = get_posts($args);
if ($attachments)
{
foreach ($attachments as $attachment)
{

if (count($attachments) === 1)
echo do_shortcode('[gallery exclude="' . $thumb_id . '" option1="value1" link="' . wp_get_attachment_image_src($attachment->ID, 'one-column')[0] . '" size="medium" columns="1"]');
if (count($attachments) === 2)
echo do_shortcode('[gallery exclude="' . $thumb_id . '" option1="value1" link="' . wp_get_attachment_image_src($attachment->ID, 'two-column')[0] . '" columns="2"]');
if (count($attachments) === 3)
echo do_shortcode('[gallery exclude="' . $thumb_id . '" option1="value1" link="' . wp_get_attachment_image_src($attachment->ID, 'three-column')[0] . '" columns="3"]');
if (count($attachments) === 4) // or if (count($attachments) => 3) if images 4 or more
echo do_shortcode('[gallery exclude="' . $thumb_id . '" option1="value1" link="' . wp_get_attachment_image_src($attachment->ID, 'four-column')[0] . '" columns="4"]');

}
}


hope this help :-)