2011-06-05 73 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;?>">