0

我只是想知道是否可以在 wordpresspagination_links()的循环中使用该函数?foreach当我尝试它时没有任何反应,我环顾四周,似乎这比我预期的要复杂一些......

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

1 回答 1

3

你为什么使用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); 
?>

然后一切都应该工作。

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

于 2012-09-28T14:12:47.753 回答