分页不使用自定义循环

时间:2010-11-30 作者:nurain

我有一个自定义循环,用于显示60天内可用的一些房地产列表。我使用以下函数调用它:

<?php 
$sixtydays = date(\'Y/m/d\', strtotime(\'+60 days\'));
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$query = new PostsOrderedByMetaQuery(array(
  \'post_type\' => array(\'post\', \'real-estate\'),
  \'meta_key\' => \'Time Available\',
  \'meta_compare\' => \'<=\',
  \'meta_value\' => $sixtydays,
  \'paged\' => $paged,
  \'orderby_meta_key\' => \'Price\',
  \'orderby_order\'    => \'ASC\'
));
?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
虽然循环工作得很好,但我无法让它分页。它显示前10篇(我的默认)文章,但不显示分页。显示所有帖子的唯一方法是通过添加\'posts_per_page\' => -1, 我在其他页面上有类似的循环,分页没有问题。与此唯一不同的是two 筛选帖子的元键。

我正在使用WP Page Navi来处理此页面和我的其余页面。我正在关闭循环并使用以下代码添加分页:

<?php endwhile; // End the loop. Whew. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
我怎样才能着手解决这个问题?

5 个回复
最合适的回答,由SO网友:PNMG 整理而成

我以前遇到过PageNavi的这个问题。我的解决方案是临时劫持$wp\\u查询变量,然后在关闭循环后重新分配它。exmaple:

<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args=array(
   \'post_type\'=>\'post\',
   \'cat\' => 6,
   \'posts_per_page\' => 5,
   \'paged\'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);

/* PageNavi at Top */
if (function_exists(\'wp_pagenavi\')){wp_pagenavi();}
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();   

/* DO STUFF IN THE LOOP */

endwhile; endif;
/* PageNavi at Bottom */
if (function_exists(\'wp_pagenavi\')){wp_pagenavi();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
最后一步是将$wp\\u query变量重新指定为原来的值,然后将查询重置回start。

*编辑:*修复了php标记。好眼力狙击手。

SO网友:eileencodes

今天早些时候我也有类似的问题。。。

您是否有自定义的帖子类型和具有相同slug的页面或帖子?意思是您拥有的网页的url/real-estate,以及在/real-estate重写的自定义帖子类型url?

如果是这样的话,你就不能让2具有相同的url,否则wordpress会感到困惑。

您可以更改url或尝试以下操作http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753. 我选择更改我的url,但有人编写了一个自定义查询来解决这个问题

SO网友:Adnan Limdiwala

我使用这个自定义分页,它的工作很好

//paginations for newsletter

     //define in function file
      function custom_pagination($numpages = \'\', $pagerange = \'\', $paged=\'\')  {

      if (empty($pagerange)) {
        $pagerange = 2;
      }

      /**
       * This first part of our function is a fallback
       * for custom pagination inside a regular loop that
       * uses the global $paged and global $wp_query variables.
       * 
       * It\'s good because we can now override default pagination
       * in our theme, and use this function in default queries
       * and custom queries.
       */

      if ($paged == \'\') {
          global $paged;
          if (empty($paged)) {
            $paged = 1;
          }
      }
      if ($numpages == \'\') {
        global $wp_query;
        $numpages = $wp_query->max_num_pages;
        if(!$numpages) {
            $numpages = 1;
        }
      }

      /** 
       * We construct the pagination arguments to enter into our paginate_links
       * function. 
       */

      $pagination_args = array(
        \'base\'            => get_pagenum_link(1) . \'%_%\',
        \'format\'          => \'page/%#%\',
        \'total\'           => $numpages,
        \'current\'         => $paged,
        \'show_all\'        => false,
        \'end_size\'        => 1,
        \'mid_size\'        => $pagerange,
        \'prev_next\'       => true,
        \'prev_text\'       => __(\'&#9668;\'),
        \'next_text\'       => __(\'&#9658;\'),
        \'type\'            => \'plain\',
        \'add_args\'        => true,
        \'add_fragment\'    => \'\',
        \'after_page_number\' => \'\',
        \'before_page_number\' =>\'\',
        );
     $paginate_links = paginate_links($pagination_args);

      if ( $paginate_links ) {
        echo "<nav class=\'custom-pagination\'>";
          //echo "<span class=\'page-numbers page-num\'>Page " . $paged . " of " . $numpages . "</span> ";
          echo $paginate_links;
        echo "</nav>";
      }
    }
    ?>
//为任何模板(如模板新闻稿)定义此

    <?php  $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
                               $newslatter_detail = array(
                               \'post_type\' => \'newsletter\',
                               \'post_status\' => \'publish\',
                               \'posts_per_page\' =>4,
                               \'order\' => \'ASC\',
                               //\'orderby\' =>\'date\',
                               \'paged\' => $paged
                               );
                               $posts = new WP_Query( $newslatter_detail );
                               $posts_array = get_posts( $newslatter_detail );
                              if ( $posts -> have_posts() ) {
                               while ( $posts->have_posts() ) : $posts->the_post();
                               the_title();
                               endwhile;
                        wp_reset_postdata();
              } else { ?>
                 No Forum List found.
                <?php } ?>

              <div class="pagination">
                    <?php
                       if (function_exists(custom_pagination)) {
                        custom_pagination($posts->max_num_pages,"",$paged);
                       }
                       ?>
                 </div>

SO网友:ChowKaiDeng

这是对我有效的解决方案,使用了nurain的部分原始代码和Jan Fabry的答案:

<?php
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$myquery = new WP_Query(
    array(
        \'posts_per_page\' => \'2\',
        \'paged\'=>$paged
        // add any other parameters to your wp_query array
    )   
);  
?>

<?php
if ($myquery->have_posts()) :  while ($myquery->have_posts()) : $myquery->the_post();
?>

<!-- Start your post. Below an example: -->

<div class="article-box">                               
<h2 class="article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="article-excerpt"><?php echo(get_the_excerpt()); ?></p>                        
</div>

<!-- End of your post -->

<?php endwhile; ?>
<?php wp_pagenavi( array( \'query\' => $myquery ) ); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
<?php wp_reset_query(); ?>
<?php else : ?>
<p>No posts found</p>
<?php endif; ?>

SO网友:Shiv Singh

您可以使用此方法显示自定义帖子类型您的分页可以正常工作!!!

<?php
  query_posts( array( \'post_type\' => \'post\', \'posts_per_page\' => \'2\', \'paged\' =>     get_query_var( \'paged\' ) ) ); 

  if (have_posts() ) :  while (have_posts() ) : the_post(); 
?>
<!-- Start your post. Below an example: -->
<div class="article-box">                               
<h2 class="article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="article-excerpt"><?php echo(get_the_excerpt()); ?></p>                        
</div>
<!-- End of your post -->
<?php endwhile; ?>
<?php wp_pagenavi(); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
<?php wp_reset_query(); ?>
<?php else : ?>
<p>No posts found</p>
<?php endif; ?>

结束

相关推荐

致命错误:对非对象调用成员函数Query()

在下面的代码(来自我的functions.php)中,我试图从wp\\u postmeta表中创建一个项目数组,其中meta\\u键为“\\u wp\\u attached\\u file”。我收到一个错误:致命错误:对非对象调用成员函数query()查询有什么问题?$excludeImages = array(); $excludeImages = $wpdb->query(\"SELECT meta_value FRO