Search code examples
phpwordpressloopsjquery-cycle2

Display portrait images in pairs using Cycle2


I'm trying to create a picture portfolio of horizontal and vertical images in Wordpress using Cycle2, where all vertical (portrait) images are displayed in pairs. The following code works but it displays every image twice, once as current and once as next. How do I skip an image if it has been displayed before? Thanks!

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_parent' => $post->ID,
    );

    $attachments = get_posts( $args );
    $length = count($attachments);
    for($i = 0; $i < $length ; ++$i) {
       $attachment = current($attachments);
       $next_attachment = next($attachments);                   
       $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'large' ); 
       $next_image_attributes = wp_get_attachment_image_src( $next_attachment->ID, 'large' ); 
       $w = $image_attributes[1];
       $h = $image_attributes[2]; 
       $nw = $next_image_attributes[1];
       $nh = $next_image_attributes[2]; 
           if($h > $w & $nh > $nw) {
               echo '<li>';
               echo wp_get_attachment_image( $attachment->ID, 'large' );
               echo wp_get_attachment_image( $next_attachment->ID, 'large' );
               echo '</li>';
           } 

Solution

  • Steve's logic is right but in order for current and next to work, a for loop needs to be used. All I needed to do is count all portrait instances and output only the odd ones. The following works:

    $args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID,
    'orderby' => menu_order,
    'order' => 'ASC'
    );
    
    $attachments = get_posts( $args );
    $length = count($attachments);
    $counter = 0;
    
    for($i = 0; $i < $length ; ++$i) {
        $attachment = current($attachments);
        $next_attachment = next($attachments);                   
        $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'large' );
        $next_image_attributes = wp_get_attachment_image_src( $next_attachment->ID, 'large' ); 
        $w = $image_attributes[1];
        $h = $image_attributes[2];
        $nw = $next_image_attributes[1];
        $nh = $next_image_attributes[2];
        if($h > $w) {
            $counter++;
            if(($nh > $nw) and ($counter % 2 == 1)) {
                echo '<li>';
                echo wp_get_attachment_image( $attachment->ID, 'large' );
                echo wp_get_attachment_image( $next_attachment->ID, 'large' );
                echo '</li>';
            } elseif(($nh < $nw) and ($counter % 2 == 1)) {
                echo '<li>';
                echo wp_get_attachment_image( $attachment->ID, 'large' );
                echo '</li>';
            } elseif((!$next_attachment) and ($counter % 2 == 1)) {
                echo '<li>';
                echo wp_get_attachment_image( $attachment->ID, 'large' );
                echo '</li>';
            }
        } elseif($h < $w) {
            echo '<li>';
            echo wp_get_attachment_image( $attachment->ID, 'large' );
            echo '</li>';
            $counter = 0;
        }       
    }