Vince P
Vince P

Reputation: 1791

Adding a css class in a foreach string

I have the following code which displays related items in a wordpress template but I would like to add a class that every second item is attached with a css class of right, what do I need to modify to achieve this?

  <?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;?>

Although this is Wordpress related I thought it was more related to general PHP coding so psoting here rather than at WPSE.

Upvotes: 0

Views: 1259

Answers (5)

PeeHaa
PeeHaa

Reputation: 72652

If the array has a sequenced index you can use either a modulo calculation or a bitwise operation. If the array is based of non-numeric or not sequenced numbers you need to add a counter.

$i & 1 // odd using bitwise

$i % 2 // odd modulo

So what you would get is the following:

$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>';
}

Or using bitwise:

$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>';
}

Or if $rel has a zero based sequenced index:

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>';
}

Upvotes: 2

Jibla
Jibla

Reputation: 813

<?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>';
 }
?>

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Do you mean:


$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;

Upvotes: 0

Vitalii Lebediev
Vitalii Lebediev

Reputation: 662

    <?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>';
       }
    ?>

Something like this. My advice is to try to use curly braces instead of foreach () : endforeach;

Upvotes: 0

Jaspreet Chahal
Jaspreet Chahal

Reputation: 2750

try this

<?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++;
}?>

Upvotes: 5

Related Questions