Title: Giving php generated divs different classes
Last modified: August 21, 2016

---

# Giving php generated divs different classes

 *  [CrispySinger](https://wordpress.org/support/users/crispysinger/)
 * (@crispysinger)
 * [12 years ago](https://wordpress.org/support/topic/giving-php-generated-divs-different-classes/)
 * I’m wondering if its possible to generate a different class for each div that
   this php code generates. Theres a total of 5 divs that are going to be displayed
   after the script has ran.
 *     ```
       <?php $top_query = new WP_Query('cat=CaseStudies&showposts=5'); ?>
       <?php while($top_query->have_posts()) : $top_query->the_post(); ?>
       <?php
         echo '<div class="col-md-2 break project-img middle no-pad-mar">';
         echo '<div class="case caseimg">';
         the_post_thumbnail( 'thumbnail' );
       ?>
         <a href>/'">
         <span class="blk"><strong>Project Title</strong></span>       <span>Location</span></a>
       <?php
         echo '</div>';
         echo '</div>';
       ?>
       <?php $count1++; ?>
       <?php endwhile; ?>
       ```
   
 * _[Moderator Note: Please post code & markup between backticks or use the code
   button. Your posted code may now have been permanently damaged by the forum’s
   parser.]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [stephencottontail](https://wordpress.org/support/users/stephencottontail/)
 * (@stephencottontail)
 * [12 years ago](https://wordpress.org/support/topic/giving-php-generated-divs-different-classes/#post-4985417)
 * What exactly are you trying to accomplish? WordPress provides a `current_post`
   object (in your case, it would be `$top_query->current_post`) that can be used.
   For example, `<div class="foo-<?php echo $top_query->current_post; ?>">` would
   give you `<div class="foo-0">`, `<div class="foo-1">`, `<div class="foo-2">`,
   etc. You could also use `current_post` in a [switch statement](http://www.php.net/manual/en/control-structures.switch.php),
   if necessary.
 *  Thread Starter [CrispySinger](https://wordpress.org/support/users/crispysinger/)
 * (@crispysinger)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/giving-php-generated-divs-different-classes/#post-4985593)
 * Sorry for the late reply. I got it working in the end by using a CSS “first-of-
   type” & “last-of-type” to get the five Divs to fit within the container properly.
 * The issue was that the boxes wouldn’t align with content above it and due to 
   a client wanting the boxes to be spot on with that content I was required to 
   edit it a bit. So I had to have the first and last divs flush with the side of
   the container and the rest aligned to suit. I’m using Roots to so that’s why 
   I needed to tweak it a bit to override some of the styles for generated column
   classes. Normally wouldn’t be a problem but because I was using a for each loop
   to display the divs I was unsure as to how to style each div the way I wanted.
 * Probably not the best fix but its working for now.
 * Cheers for the help!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Giving php generated divs different classes’ is closed to new replies.

## Tags

 * [php](https://wordpress.org/support/topic-tag/php/)
 * [thumbnail images](https://wordpress.org/support/topic-tag/thumbnail-images/)
 * [while loop](https://wordpress.org/support/topic-tag/while-loop/)

 * 2 replies
 * 2 participants
 * Last reply from: [CrispySinger](https://wordpress.org/support/users/crispysinger/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/giving-php-generated-divs-different-classes/#post-4985593)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
