【问题标题】:Display portrait images in pairs using Cycle2使用 Cycle2 成对显示肖像图像
【发布时间】:2015-03-19 11:55:08
【问题描述】:

我正在尝试使用 Cycle2 在 Wordpress 中创建水平和垂直图像的图片组合,其中所有垂直(纵向)图像成对显示。以下代码有效,但它显示每个图像两次,一次作为当前图像,一次作为下一个图像。如果之前显示过图像,如何跳过该图像?谢谢!

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

【问题讨论】:

    标签: php wordpress loops jquery-cycle2


    【解决方案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>';
    

    【讨论】:

    • 谢谢,但作品集不仅包含肖像图像,因此我需要包含某种声明,仅跳过肖像图像。
    • @MrArkadin 啊,是的,错过了 - 我已经进行了编辑,所以计数器只增加了投资组合图像。
    • 我试图完成这项工作,但似乎“下一个”和“当前”在 foreach 循环中无法按预期工作,因此我无法计算下一个附件的尺寸跨度>
    【解决方案2】:

    Steve 的逻辑是正确的,但为了使 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;
        }       
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-22
      • 2018-04-29
      • 2014-07-03
      • 2015-03-24
      • 2011-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多