开发者

specific divider after posts

I would like to have a specific divider after my posts.

I have 3 dividers -> divider1,divider2,divider3

Basicly i would like for them to loop, so after post 1 i get divider1, post 2-> divider2, post3->divider3, post 4 ->divider1 etc...

This is my code:

<div class="post">
              &l开发者_如何转开发t;div class="post-title">
                  <h2><a href="<?php the_permalink() ?>" rel="bookmark"
                         title="Permanent Link to  <?php the_title_attribute(); ?>">
                      <?php the_title(); ?>
                  </a></h2>
              </div>
              <div class="post-date">
                  <?php the_time('j-n-Y') ?><br/>
              </div>

              <div class="entry">
                  <?php the_excerpt();?>
              </div>
              <div class="meta">
                  <span class="post-cat"><?php the_category(',') ?></span>
                  <span class="post-comments"> <?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?></span>
              </div>
              <div class="divider1"></div>
          </div>

so the class should loop from divider1 to divider2 to divider3 to divider1 and keep on looping, can anyone help me?


Make a counter and check its modulus to cycle 1 to 3 (actually we'll use 0 to 2)

// Start counter at 0
$counter = 0;
// This is your loop
foreach ($posts as $post) {

  // Now a switch will determine which divider to use.
  // We'll select class divider1 divider2 or divider3 based 
  // on the current counter position. This will keep looping no
  // matter how many posts you have
  switch ($counter % 3) {
    case 0: // output divider 1
      $divider_class = 'divider1';
      break;
    case 1: // output divider 2
      $divider_class = 'divider2';
      break;
    case 2: // output divider 3
      $divider_class = 'divider3';
      break;
  }

  // All your HTML is output here...
  // Skipping most of it for brevity, but the important part is the divider...
  <div class="<?php echo $divider_class;?>"></div>

  // EDIT Forgot the important step of incrementing the counter...
  $counter++;

// Don't forget to close your loop
}

There are other ways you could do this. For example, without the switch you can set the divider class directly with the modulus:

// Alternate method - no switch and less typing but less flexible
$divider_class = "divider" . ($counter % 3);

<div class="<?php echo $divider_class;?>">
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜