2015-03-19 53 views
2

我正嘗試在Wordpress中使用Cycle2創建水平和垂直圖像的圖片組合,其中所有垂直(人像)圖像都成對顯示。下面的代碼有效,但它顯示每個圖像兩次,一次爲當前,一次爲下一次。如果之前已經顯示過圖像,我該如何跳過圖像?謝謝!使用Cycle2以成對方式顯示人像圖像

$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>'; 
      } 

回答

1

史蒂夫的邏輯是正確的,但爲了currentnext工作,一個for循環需要使用。我需要做的就是統計所有肖像實例並輸出唯一的實例。以下作品:

$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; 
    }  
} 
1

最簡單的方法是運行規律循環,只有輸出li元素每隔一個循環:

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

$attachments = get_posts($args); 
echo '<li>'; 
$counter=0; 
foreach($attachments as $attachment){ 


    $image_attributes = wp_get_attachment_image_src($attachment->ID, 'large'); 
    $w = $image_attributes[1]; 
    $h = $image_attributes[2]; 
    if($h > $w) { 
     $counter++; 
     echo wp_get_attachment_image($attachment->ID, 'portfolio'); 
     if($counter %2 == 0){ 
      echo '</li><li>'; 
     } 
    } 

} 
echo '</li>'; 
+0

謝謝,但投資組合將不僅包含肖像圖像,所以我需要包括某種只有肖像圖像被跳過的聲明。 – MrArkadin 2015-03-19 12:57:45

+0

@MrArkadin啊,是的,錯過了 - 我做了一個編輯,所以櫃檯只增加了投資組合圖片。 – Steve 2015-03-19 13:02:50

+0

我試圖做這項工作,但似乎「下一個」和「當前」在foreach循環內沒有按預期工作,因此我無法計算下一個附件的尺寸 – MrArkadin 2015-03-19 15:36:25