总图像宽度为while

时间:2012-06-09 17:40:59

标签: php image while-loop conditional

我有一些php代码,可以回显WordPress中特定图像大小的宽度。目前,如果图像宽度大于80,那么它回声“青蛙”...我想要它做的是计算我的所有图像宽度,如果这些图像的总数大于600(假设数字)然后回声“青蛙”。我正在使用的代码看起来像(我正在使用此代码):

<?php
    $image = wp_get_attachment_image_src (get_post_thumbnail_id($post_id), 'gallery-thumbnail');
    list($width) = getimagesize($image[0]);
        echo $width;
    if( $width > 80 ) {
        echo "frog";
    }
?>

我的基本WordPress标准是:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <!-- some code here -->
<?php endwhile; else: ?>
    <!-- some code here -->
<?php endif; ?>

有什么想法吗?

谢谢,乔希

1 个答案:

答案 0 :(得分:0)

部分伪代码/部分解决方案:

$sumOfWidths = 0;

foreach($images as $image)
{
    $sumOfWidths = $sumOfWidths + $image['width'];
}  

if($sumOfWidths>600)
{
    echo 'frog';
}

foreach($images as $image) { $sumOfWidths = $sumOfWidths + $image['width']; } if($sumOfWidths>600) { echo 'frog'; }

只需将上面的代码循环遍历每个图像,然后将图像宽度添加到变量。

foreach循环完成后, $sumOfWidths中应该有一个数字,您可以检查,然后根据需要采用您的逻辑。

看一下这个code snippet让你入门