2012-12-10 113 views
1

JetPack by Automatic是一款为WordPress用户提供的免费图像编辑SaaS工具。其众多功能之一是,您可以更改正在返回的图像url的子域,以增强多个srcs的并行加载。三个域的选项是:“i0”,“i1”和“i2”。如何更改字符串每第n次出现在php

如果每张照片出现在一个foreach语句我怎么能这三个子域之间的转动,使IMG1 & IMG4使用“IO”,IMG2 & IMG5使用“I1”和IMG3使用“I2”?

foreach ($images as $attachment) { 

    // If using Photon select full-size photos 
    if ($use_photon == 'one') 
     print $before_img . '<img src="http://i1.wp.com/'. str_replace('http://', '', $$fullsizeurl) . $resize_img_to .'">' . $after_img; 

    // Else select 'medium' thumbnail 
    else 
     print $before_img . wp_get_attachment_image($attachment->ID, 'medium') . $after_img; 

} // foreach 

回答

2

在图像的索引上使用'%'运算符。这是做到这一点的最好方式。

foreach ($images as $i => $attachment) { 

    $host = 'i' + ($i % 3); 
    // If using Photon select full-size photos 
    if ($use_photon == 'one') 
     print $before_img . '<img src="http://'. $host .'.wp.com/'. str_replace('http://', '', $$fullsizeurl) . $resize_img_to .'">' . $after_img; 

    // Else select 'medium' thumbnail 
    else 
     print $before_img . wp_get_attachment_image($attachment->ID, 'medium') . $after_img; 

} // foreach 
+0

太棒了,学到了新东西。谢谢! – torinagrippa

相关问题