Support » Themes and Templates » Giving php generated divs different classes

  • CrispySinger


    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(); ?>
      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>
      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)
  • Moderator stephencottontail


    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, if necessary.

    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.