帖子后的特定分隔符

时间:2011-06-05 11:37:58

标签: php wordpress

我希望在帖子之后有一个特定的分隔符。

我有3个分频器 - > divider1,DIVIDER2,divider3

基本上我想让他们循环,所以在帖子1后我得到divider1,post 2-> divider2,post3-> divider3,post 4 - > divider1等...

这是我的代码:

<div class="post">
              <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>

所以类应该从divider1循环到divider2到divider3到divider1并继续循环,有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

制作一个计数器并检查其模数为1至3周期(实际上我们将使用0到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
}

还有其他方法可以做到这一点。例如,如果没有开关,您可以直接使用模数设置分隔线类:

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

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