[英]add pagination to “for each” loop in wordpress
Im just wondering is it possible to use the pagination_links()
function in a foreach
loop in wordpress? 我只是想知道是否可以在wordpress的foreach
循环中使用pagination_links()
函数? 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. 在这方面的任何帮助将非常感谢。
EDIT: I have decided to change the code to this to suit wordpress... 编辑:我决定将代码更改为此以适合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; ?>
Why are you using foreach
instead of while
? 为什么要使用foreach
而不是while
?
The default loop with pagenation should look like this (should work with foreach as well): 带有分页的默认循环应如下所示(也应与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; ?>
This just shows the next
and previous
link, but if you want pagination with numbers, I would suggest the great plugin: Wp-Pagenavi . 这仅显示了next
和previous
链接,但是如果您想使用数字分页,我建议使用很棒的插件: Wp-Pagenavi 。
Good luck! 祝好运!
EDIT: 编辑:
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);
?>
Then everything should work. 然后一切都会正常。
You can find more information in the codex: http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query 您可以在编解码器中找到更多信息: http : //codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.