当前位置: 首页>>技术教程>>正文


分页不适用于自定义循环

webfans 技术教程 , , , , 去评论

问题描述

我有一个自定义循环,我用来显示一些房地产列表,将在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,在一个页面上显示它我在其他没有分页问题的页面上有类似的循环。与此相关的唯一区别是有两个元键过滤帖子。

我正在使用WP Page Navi和其他页面。我正在关闭循环并使用以下代码添加分页:

<?php endwhile; // End the loop. Whew. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>

我该怎么办呢?

最佳解决方案

我以前用PageNavi遇到过这个问题。我的解决方案是临时劫持$ wp_query变量,然后在关闭循环后重新分配它。一个例子:

<?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_query变量重新分配给最初的变量,然后将查询重新设置为start。

*编辑:*修复了php标签。好眼睛狙击手。

次佳解决方案

我今天早些时候遇到过类似的问题……

您是否有自定义帖子类型以及具有相同slug的页面或帖子?含义是你有/real-estate页面的网址和/real-estate自定义帖子类型网址重写?

如果是这种情况你就不能拥有2个相同的网址,否则wordpress会感到困惑。

您可以更改网址或尝试此http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753。我选择更改我的网址,但有人在那里写了一个自定义查询来解决问题

第三种解决方案

我正在使用这个自定义分页和它的工作正常

//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'       => __('◄'),
        'next_text'       => __('►'),
        '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>";
      }
    }
    ?>

//为template-newsletter等任何模板定义此项

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

第四种方案

这是一个对我有用的解决方案,使用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; ?>

参考资料

本文由朵颐IT整理自网络, 文章地址: https://duoyit.com/article/2171.html,转载请务必附带本地址声明。