将分页添加到“对于每个"在wordpress中循环 [英] add pagination to "for each" loop in wordpress

查看:61
本文介绍了将分页添加到“对于每个"在wordpress中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否可以在wordpress的foreach循环中使用pagination_links()函数? 当我尝试没有任何反应时,我环顾四周,看来这比我预期的要棘手...

Im just wondering is it possible to use the pagination_links() function in a foreach loop in wordpress? When I try it nothing happens, I have looked around and it seems this is a little trickier than I was expecting...

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

我基本上是在寻找基本分页,带有下一个",上一个"和数字.

Im basically looking for basic pagination, with "next", "prev" and numbers.

对此有任何帮助,将非常感谢.

Any help on this would be great thanks.

我决定将代码更改为适合wordpress ...

I have decided to change the code to this to suit 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一起使用):

The default loop with pagenation should look like this (should work with foreach as well):

<?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链接,但是如果您想使用数字分页,我建议使用很棒的插件:

This just shows the next and previous link, but if you want pagination with numbers, I would suggest the great plugin: Wp-Pagenavi.

祝你好运!

您遇到的错误是您没有正确设置页面变量.您需要执行以下操作:

The error you are experiencing is that you haven't set the paged variable correctly. You need to do the following:

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

You can find more information in the codex: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

这篇关于将分页添加到“对于每个"在wordpress中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆