2011-10-18 30 views
0

這是我的檔案頁面代碼如何在歸檔頁面創建多個循環?

我希望避免在第四個環&重複的帖子,我想在第3圈數後

有4個迴路

1第一循環次數:1後

<?php 
    $count = 1; 
    if (have_posts()) : while (have_posts()) : the_post();    
    if($count == 1) : ?> 

<a href="<?php the_permalink() ?>" rel="bookmark" title="" ><?php the_title(); ?></a> 

2秒環 - - 計數= 1交

<?php if (have_posts()) : ?> 
<?php $count = 1; ?> 
<?php while (have_posts()) : the_post(); ?> 
<?php $count++; ?> 
<?php if ($count == 2) : ?> 
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?> </a> 

3第三loop--計數= 5交

<?php else : ?> 
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a> 
<?php endif; ?> 
<?php endwhile; ?> 

<?php else : ?> 
<h1>Most Viewed News</h1> 
<?php endif; ?> 

4環 - 計數=所有的其餘部分後

<?php else : ?> 
<a href="<?php the_permalink(); ?>" title=""><?php the_title(); ?></a> 

- 廣告計數 -

<?php if ($count == 3 || $count == 5) : ?> 
<?php dt_show_ads();?> 
<?php endif; ?> 

<?php endif; ?> 
<?php $count++; ?> 
<?php endwhile; ?> 


<?php else : ?> 
<div class="post"> 
<h2 class="archiveTitle"><?php _e('Sorry','linepress');?></h2> 
</div> 
<?php endif; ?> 

回答

0

要避免循環中的重複帖子,你可以設置數組中的循環帖子,並使用in_array函數來檢查帖子是否循環。

<?php 

$do_not_duplicate = array(); 

// first loop . 
if (have_posts()) : 
    while (have_posts()) : the_post(); 
    $do_not_duplicate[] = get_the_id(); 
    echo "<a href='LINK'>TITLE</a>"; 
    endwhile; 
endif; 

// 2nd loop . 
if (have_posts()) : 
    while (have_posts()) : the_post(); 
    if (!in_array (get_the_id() , $do_not_duplicate)) { 
     echo "<a href='LINK'>TITLE</a>"; 
     $do_not_duplicate[] = get_the_id(); 
    } 
    endwhile; 
endif; 
+0

請問您可以修改代碼嗎?我很困惑 – atali

+0

在這裏附加文件,我會爲你修改。 –

+0

這裏是文件http://pastebin.com/Cbg95nDY。非常感謝您的幫助 您可以看到存檔頁面中存在重複的帖子 – atali

相關問題