【问题标题】:query_posts with pagination not workingquery_posts 分页不起作用
【发布时间】:2011-01-10 16:25:51
【问题描述】:

我正在尝试使用以下代码进行分页,但没有运气:

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'cat' => '5',
    'post_type' => 'post',
    'posts_per_page' => 6,
    'paged' => $page,
);

query_posts($args);

while(have_posts()) {
    the_post(); ?>
    <div class="project_item">
        <div class="dotted">
            <hr />
        </div>
        <div class="project_thumb"><a href="<?php echo get_permalink(); ?>"><img src="<?php getCustomField('news_thumbnail'); ?>" /></a></div>
        <div class="project_entry"><h4><a href="<?php echo get_permalink(); ?>"><?php getCustomField('news_title'); ?></a></h4>
            <?php getCustomField('news_excerpt'); ?>
            <a href="<?php echo get_permalink(); ?>" class="readmore">Read more..</a>
        </div>
    </div>
<?php }

wp_reset_query();  // Restore global post data

【问题讨论】:

  • 您可能想尝试通过wordpress.stackexchange.com 询问这个问题。
  • 每周有多少新的 stackexhange 站点开放?谢谢!

标签: php wordpress


【解决方案1】:

我只需要在循环之后添加上一个和下一个链接:

<div class="navigation">
  <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
  <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>

完整代码:

<?php
     $args = array(
                   'cat' => '5',
                   'post_type' => 'post',
                   'posts_per_page' => 6,
                   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
                   );

    query_posts($args);

while (have_posts()) : the_post();
 /* Do whatever you want to do for every page... */
?>
    <div class="project_item">
        <div class="dotted">
          <hr />
        </div>
        <div class="project_thumb"><a href="<?php echo get_permalink(); ?>"><img src="<?php getCustomField('news_thumbnail'); ?>"  /></a></div>
        <div class="project_entry"><h4><a href="<?php echo get_permalink(); ?>"><?php getCustomField('news_title'); ?></a></h4>
          <?php getCustomField('news_excerpt'); ?>
          <a href="<?php echo get_permalink(); ?>" class="readmore">Read more..</a> </div>
      </div>

      <?php

endwhile;
?><div class="navigation">
  <div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
  <div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>
<?php
wp_reset_query();  // Restore global post data
?>

【讨论】:

    【解决方案2】:

    这是让它与所有分页插件一起使用的修复:

    global $wp_query;
    query_posts(
        array_merge(
            array(
            'category__not_in' => 69, //ifu  want to exclude some category
            'posts_per_page' => 9
             ),
            $wp_query->query
        )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 2013-04-23
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      相关资源
      最近更新 更多