2012-09-28 21 views
0

我只是想知道是否可以在wordpress中的foreach循環中使用pagination_links()函數? 當我嘗試它沒有任何反應,我環顧四周,似乎這比我期待有點麻煩......在wordpress中爲每個循環添加分頁

<?php 
$args = array('numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); 
$postslist = get_posts($args); 
foreach ($postslist as $post) : setup_postdata($post); ?> 
    <div class="events"> 
     <div class="newslistingblock"> 
     <div class="newslistingblockheader"><p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p> 
     </div> 
     <div class="newslistingblockthumbnail"> 
     <?php echo get_the_post_thumbnail($post_id, 'news-thumb', $attr); ?> </div> 
     <div class="newslistingexcerpt"> 
       <?php the_excerpt(); ?> </div> 
    </div> 
    </div> 



<?php endforeach; ?> 

林基本上找的基本分頁,以「下一個」,「上一頁」和數字。

任何幫助,這將是非常感謝。

編輯: 我已經決定更改代碼以使之適合wordpress的...

<?php 
query_posts('posts_per_page=5'); 
if (have_posts()) : 

while (have_posts()) : the_post(); ?> 
<!-- Do suff --> 

    <div class="events"> 
     <div class="newslistingblock"> 
     <div class="newslistingblockheader"><p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p> 
     </div> 
     <div class="newslistingblockthumbnail"> 
     <?php echo get_the_post_thumbnail($post_id, 'news-thumb', $attr); ?> </div> 
     <div class="newslistingexcerpt"> 
       <?php the_excerpt(); ?> </div> 
    </div> 
    </div> 
    <?php endwhile; ?> 
    <div class="navigation"> 
    <div class="alignleft"><?php next_posts_link('← Older Entries') ?></div> 
    <div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div> 
</div> 
<?php endif; ?> 
+0

張貼代碼嘗試發佈一個完整的塊時!在您的發佈代碼中,我無法看到您關閉「foreach」的位置.. – Deepak

+0

哪裏試圖迴應您的分頁(並結束您的前期,就此而言)? –

+0

用'var_dump' /'print_r'查看$ post中的內容,並自己動手:) – Bokw

回答

3

你爲什麼要使用foreach代替while

與pagenation默認的循環應該是這樣的(應該用foreach工作以及):

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <!-- Do suff --> 
<?php endwhile; ?>  
<div class="navigation"> 
    <div class="alignleft"><?php next_posts_link('← Older Entries') ?></div> 
    <div class="alignright"><?php previous_posts_link('Newer Entries →') ?></div> 
</div> 
<?php endif; ?> 

這只是顯示了nextprevious鏈接,但如果你想用數字分頁,我建議很棒的插件:Wp-Pagenavi

祝你好運!

編輯:

您所遇到的錯誤是你沒有正確設置分頁變量。你需要做到以下幾點:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 

query_posts('posts_per_page=5&paged=' . $paged); 
?> 

然後一切都應該工作。

可以在食品找到更多的信息:http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

+0

謝謝Krycke,我改變了代碼(如上所示),但不幸的是,即使下一個/ prev出現,他們不真的工作,在相同的帖子相同的網頁繼續加載。 – Adrian

+0

同意 - WP-pagenavi是我在幾乎所有安裝中所使用的默認插件之一。優秀,易於風格。 – McNab

+0

好吧生病嘗試插件,看看我能做些什麼謝謝。 – Adrian

相關問題