2012-03-02 55 views
0

我有下面的代碼,它顯示wordpress模板中的相關項目,但我想添加一個類,每個第二個項目都附加了一個css類right,我需要什麼修改來實現這個?在一個foreach字符串中添加一個css類

<?php $rel = $related->show(get_the_ID(), true); 
    foreach ($rel as $r) : 
    echo '<div class=related-item><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
    endforeach;?> 

雖然這是WordPress的關係我想這是更多的與一般的PHP編碼所以psoting這裏,而不是在WPSE。

回答

5

試試這個

<?php $rel = $related->show(get_the_ID(), true); 
$count = 0; 
foreach ($rel as $r) { 
$class= ($count%2 == 0)?"right":""; 

echo '<div class="related-item '.$class.'"><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
$count++; 
}?> 
+0

丟棄了代碼並且頁面沒有工作,在Dreamweaver中查看提示它告訴我在此編碼的第四行有一個語法錯誤... – 2012-03-02 09:43:52

+0

以及語法錯誤已得到修復 – 2012-03-02 09:44:37

+0

完美,謝謝 – 2012-03-02 09:48:06

0
<?php $rel = $related->show(get_the_ID(), true); 
     $i = 0; 
     foreach ($rel as $r) { 
      echo '<div class=related-item' . ($i++ % 2 ? '' : ' right') . '><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
     } 
    ?> 

這樣的事情。我的建議是嘗試使用花括號代替foreach():endforeach;

0

你的意思是:

 

$i = 0; 
    foreach ($rel as $r) : 
     $class = (($i % 2) == 0) ? "your_class" : ""; 
      echo '<div class="related-item $class"><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.''.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
     $i++; 
    endforeach; 
 
2

如果陣列有一個序列索引,你可以使用一個模運算或按位運算。如果該數組基於非數字或未排序的數字,則需要添加一個計數器。

$i & 1 // odd using bitwise 

$i % 2 // odd modulo 

那麼,你會得到如下:

$i = 0; 
foreach ($rel as $r) { // note that I have used curly brackets. I think it is cleaner more standard 
    $i++; 
    $classes = array('related-item'); 
    if ($i % 2 == 0) $classes[] = 'right'; 

    echo '<div class="'.implode(' ', $classes).'"><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
} 

或者使用逐:

$i = 0; 
foreach ($rel as $r) { // note that I have used curly brackets. I think it is cleaner more standard 
    $i++; 
    $classes = array('related-item'); 
    if ($i & 2 == 0) $classes[] = 'right'; 

    echo '<div class="'.implode(' ', $classes).'"><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
} 

或者,如果$rel具有零基於序列索引:

foreach ($rel as $index => $r) { // note that I have used curly brackets. I think it is cleaner more standard 
    $classes = array('related-item'); 
    if ($index & 2 == 1) $classes[] = 'right'; 

    echo '<div class="'.implode(' ', $classes).'"><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
} 
0
<?php 
foreach($rel as $key => $r) { 
    /* here we check if $key is even and assign class name or empty string to $class variable */ 
    ($key%2) ? $class = 'your-class-name' : $class = ''; 

    /* and here we just add $class variable to 'class="related-items " part. */ 
    /* so if $key is odd then $class will be empt and your div will have only 'related-item' class, and if $key is even then $class will hold 'your-class-name' value and div will have two classes: related-item and your-class-name */ 
    echo '<div class="related-item '.$class.'"><a href='.get_permalink($r->ID).'>'.'<div class=page-related-title>'.$r->post_title.'</div>'.get_the_post_thumbnail($r->ID, array(50,50)).'</a></div>'; 
} 
?> 
相關問題