【问题标题】:foreach loop - repeat foreach or store/echo result for second loopforeach 循环 - 为第二个循环重复 foreach 或存储/回显结果
【发布时间】:2015-06-26 04:36:29
【问题描述】:

这是关于从答案中过滤 WordPress 的本机图库输出:here

// Now you loop through each attachment
foreach ($attachments as $id => $attachment) {
// Fetch all data related to attachment 
$img = wp_prepare_attachment_for_js($id);

// If you want a different size change 'large' to eg. 'medium'
$url = $img['sizes']['large']['url'];
$height = $img['sizes']['large']['height'];
$width = $img['sizes']['large']['width'];
$alt = $img['alt'];

// Store the caption
$caption = $img['caption'];

$output .= "<li>\n";
$output .= "<img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" />\n";

// Output the caption if it exists
if ($caption) { 
    $output .= "<div class=\"orbit-caption\">{$caption}</div>\n";
}

$output .= "</li>\n";
}

$output .= "</ul>\n";
$output .= "</div>\n";

return $output;

}

如果再次需要结果,是否有更有效的方法来重复 foreach 循环输出,而不是仅仅重复 foreach 循环?

(可以在 foreach 循环中使用不同的变量,但不确定破坏串联输出是否合乎逻辑。)

希望有洞察力的逻辑或方法。

【问题讨论】:

    标签: php arrays wordpress loops foreach


    【解决方案1】:

    您不必像在函数中那样在 foreach 中返回 $output。您可以在相同访问级别的循环外使用该变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      相关资源
      最近更新 更多