簡體   English   中英

在wordpress的“ for each”循環中添加分頁

[英]add pagination to “for each” loop in wordpress

我只是想知道是否可以在wordpress的foreach循環中使用pagination_links()函數? 當我嘗試沒有任何反應時,我環顧四周,看來這比我預期的要復雜一些。

<?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; ?>

為什么要使用foreach而不是while

帶有分頁的默認循環應如下所示(也應與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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM