2012-03-26 40 views
-4

我有以下的環...內循環迴路引起不希望輸出

for ($i = 1; $i <= 10; $i++) { 
    echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>"; 
} 

內...

while ($query->have_posts()) : $query->the_post(); 
if ($keys = get_post_custom_keys()) { 
    echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><span class='card-title'>"; 
    echo the_title(); 
    echo "</span>"; 

    foreach ((array) $keys as $key) { 
    $keyt = trim($key); 
    if ('_' == $keyt{0} || 'pricing' == $keyt || 'vehicleType' == $keyt || 'coverageRegion' == $keyt || 'locationType' == $keyt) 
     continue; 
    $values = array_map('trim', get_post_custom_values($key)); 
    $value = implode($values,', '); 
    for ($i = 1; $i <= 10; $i++) { 
       echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>"; 
      } 

    } 
    echo "\n"; echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';echo '<img src="wp-content/themes/cafc/images/cards/dummy.png" />';  echo the_excerpt()."</div>";} 
    $i++; 

ENDWHILE;

然而,當我執行我的代碼,說我的while()循環返回4個值,我對()循環然後輸出同樣的事情10,在瀏覽器中的顯示爲...

enter image description here

所有我想做的事是每個<span class="srch-val'>是每個「S-RCH-VAL」下課後添加一個數字,所以S-RCH-VAL-1,S-RCH-VAL-2等..

+1

「每個」 是什麼? – bfavaretto 2012-03-26 15:40:33

+0

你能舉一個你想要的輸出的例子嗎?很明顯,for循環會打印10次相同的東西,因爲$ value不會改變。 – flo 2012-03-26 15:42:42

+0

您的意思是免費設置? – 2012-03-26 15:43:04

回答

2

你有你的foreach循環中的額外的循環。刪除這個循環,直接在foreach循環中執行echo,每次使用時都增加$i

像這樣:

$i = 1; 
while ($query->have_posts()) 
{ 
    $query->the_post(); 
    if ($keys = get_post_custom_keys()) 
    { 
     echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><span class='card-title'>"; 
     echo the_title(); 
     echo "</span>"; 

     foreach ((array) $keys as $key) 
     { 
      $keyt = trim($key); 
      if ('_' == $keyt{0} || 'pricing' == $keyt || 'vehicleType' == $keyt || 'coverageRegion' == $keyt || 'locationType' == $keyt) 
       continue; 
      $values = array_map('trim', get_post_custom_values($key)); 
      $value = implode($values,', '); 

      echo '<span class="srch-val-'.$i.'">'.apply_filters(" $value\n", $value)."</span>"; 
      $i++; // move the incrementer here so that you only increment when you actually use it. 
     } 

     echo "\n"; echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';echo '<img src="wp-content/themes/cafc/images/cards/dummy.png" />';  echo the_excerpt()."</div>"; 
    } 
} 
0

您在創建一個變量你的foreach循環的開始(比如$counter == 1;

比你的for循環應該是:

for($i = $counter; $i <= $counter+10; $i++){ 
    //do your span-class thing here 
} 

,並在foreach循環結束做到:$counter += 10;