2011-06-05 20 views
1

我想在我的帖子后有一个特定的分隔符。帖子后的特定分隔符

我有3个分隔 - > divider1,DIVIDER2,divider3

Basicly我想为他们循环,所以后1后,我得到divider1,张贴2-> DIVIDER2,post3-> divider3,后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,不断循环,谁能帮助我?

回答

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;?>"> 
相关问题