Assign CSS class to first and third html element

I am using a wordpress plugin that displays recent posts. I’ve changed the template so that the 3 most recent posts will be displayed in featured boxes on the home page. The problem is the first box needs to be assigned the ‘alpha’ class and the lastt box needs to be assigned the ‘omega’ class in order for them to fit correctly. My PHP skills aren’t this advanced.

The template is in a template.php page. And the majority of the code is as follows:

Read More
/**
 * The format for templates changed since version 0.17.
 * Since this code is included inside CatListDisplayer, $this refers to
 * the instance of CatListDisplayer that called this file.
 */

/* This is the string which will gather all the information.*/
$lcp_display_output = '';

// Show category link:
$lcp_display_output .= $this->get_category_link('strong');

//Add 'starting' tag. Here, I'm using an unordered list (ul) as an example:
$lcp_display_output .= '<div class="lcp_catlist info_box_area row clearfix" id="box_area_0">';

/**
 * Posts loop.
 * The code here will be executed for every post in the category.
 * As you can see, the different options are being called from functions on the
 * $this variable which is a CatListDisplayer.
 *
 * The CatListDisplayer has a function for each field we want to show.
 * So you'll see get_excerpt, get_thumbnail, etc.
 * You can now pass an html tag as a parameter. This tag will sorround the info
 * you want to display. You can also assign a specific CSS class to each field.
 */

foreach ($this->catlist->get_categories_posts() as $single){
  //Start a List Item for each post:
  $lcp_display_output .= '<div class="one-third column info_box alpha omega">';

  //Post Thumbnail
  $lcp_display_output .= $this->get_thumbnail($single);

  //Show the title and link to the post:
  $lcp_display_output .= $this->get_post_title($single);

  //Show comments:
  $lcp_display_output .= $this->get_comments($single);

  //Show date:
  $lcp_display_output .= ' ' . $this->get_date($single, 'span', 'lcp-date');

  //Show author
  $lcp_display_output .= $this->get_author($single);

  //Custom fields:
  $lcp_display_output .= $this->get_custom_fields($this->params['customfield_display'], $single->ID);

  /**
   * Post content - Example of how to use tag and class parameters:
   * This will produce:<p class="lcp_content">The content</p>
   */
  $lcp_display_output .= $this->get_content($single, 'p', 'lcp_content');

  /**
   * Post content - Example of how to use tag and class parameters:
   * This will produce:<div class="lcp_excerpt">The content</div>
   */
  $lcp_display_output .= $this->get_excerpt($single, 'div', 'lcp_excerpt');

  //Close li tag
  $lcp_display_output .= '</div>';

}

$lcp_display_output .= '</div>';

My first thought was to auto-increment a class to each div element and then format those specifically, but if there is an easier way to add the alpha and omega classes directly, that would be great!!!

Related posts

Leave a Reply

3 comments

  1. To do this in PHP you can just add conditional to your loop…

    $i = 1;
    
    foreach ($this->catlist->get_categories_posts() as $single){
      //Start a List Item for each post:
      if ($i == 1)
        $lcp_display_output .= '<div class="one-third column info_box alpha">';
      elseif ($i == 3)
        $lcp_display_output .= '<div class="one-third column info_box omega">';
      else
        $lcp_display_output .= '<div class="one-third column info_box">';
    
      // the rest of your foreach code
      ...
    
      $i++;
    
    }
    
  2. Why no simple try this:

    $post_position = 1;
    
    foreach ($this->catlist->get_categories_posts() as $single){
      //Start a List Item for each post:
      if ($post_position == 1){
          $lcp_display_output .= '<div class="one-third column info_box alpha">';
      }elsif ($post_position == 3){
          $lcp_display_output .= '<div class="one-third column info_box omega">';
      }else{
          $lcp_display_output .= '<div class="one-third column info_box">';
      }
      //Post Thumbnail
      $lcp_display_output .= $this->get_thumbnail($single);
    
      //Show the title and link to the post:
      $lcp_display_output .= $this->get_post_title($single);
    
      //Show comments:
      $lcp_display_output .= $this->get_comments($single);
    
      //Show date:
      $lcp_display_output .= ' ' . $this->get_date($single, 'span', 'lcp-date');
    
      //Show author
      $lcp_display_output .= $this->get_author($single);
    
      //Custom fields:
      $lcp_display_output .= $this->get_custom_fields($this->params['customfield_display'], $single->ID);
    
      /**
       * Post content - Example of how to use tag and class parameters:
       * This will produce:<p class="lcp_content">The content</p>
       */
      $lcp_display_output .= $this->get_content($single, 'p', 'lcp_content');
    
      /**
       * Post content - Example of how to use tag and class parameters:
       * This will produce:<div class="lcp_excerpt">The content</div>
       */
      $lcp_display_output .= $this->get_excerpt($single, 'div', 'lcp_excerpt');
    
      //Close li tag
      $lcp_display_output .= '</div>';
    
      $post_position++;
    }