【问题标题】:Wordpress custom wp_pagenavi does not work on custom page with custom queryWordpress 自定义 wp_pagenavi 不适用于具有自定义查询的自定义页面
【发布时间】:2013-05-18 07:29:31
【问题描述】:

我正在尝试做一些自定义页面,我没有使用 wp_pagenavi 插件,而是使用自定义页面的自定义函数,现在它只适用于 index.php 页面,但在我之前几天前它工作正常添加了更多查询元素。

//custom pagepavi function
function my_pagenavi( $the_query = false ){
  global $wp_query;
  $query = ($the_query) ? $the_query : $wp_query;
  $max = $query->max_num_pages;
  $current_page = max(1, get_query_var('paged')); 
  $big = 999999999; 
  if ( $max > 1 ) { 
        echo "<div class='pagination' style='height:auto'>";
        echo paginate_links(array(
              'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),  
              'format' => '?paged=%#%',
              'current' => $current_page,
              'show_all'     => false,
              'total' => $max, 
              'type' => 'list',
              'prev_text' => __('PREV','dnp_theme'),
              'next_text' => __('NEXT','dnp_theme'), 
        ));  
        echo "</div>";
  }

}

现在这是我的模板页面中的循环。

  //some query stuff
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $query = 'offset=0&paged='.$paged;

  $blogs = new WP_Query($query);

  if ( $blogs->have_posts() ) : ?>              

        <?php /* Start the Loop */ ?>

        <?php while ( $blogs->have_posts() ) : $blogs->the_post(); ?>
              <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; ?>

        <?php my_pagenavi( array('query' => $blogs) ); ?>
  <?php endif; ?>

为什么没有加载任何东西?怎么回事??

【问题讨论】:

    标签: wordpress navigation


    【解决方案1】:

    查询不正确,这就是它不工作的原因,至少它不是,不是我让它工作正常:)

    而不是

    $blogs = new WP_Query($query);
    

    所有应该是的

    $blogs = query_posts( 'post_type=post&posts_per_page=4&paged='.get_query_var('paged') );
    

    循环和其他循环一样,完美运行

      if ( have_posts() ) : ?>              
    
            <?php /* Start the Loop */ ?>
    
            <?php while ( have_posts() ) : the_post(); ?>
                  <?php get_template_part( 'content', get_post_format() ); ?>
            <?php endwhile; ?>
    
            <?php my_pagenavi(); ?>
      <?php endif; ?>
    

    【讨论】:

      【解决方案2】:

      写完这段代码后

      全局 $wp_query;

      $大 = 999999999; // 需要一个不太可能的整数

      回显分页链接(数组(

      'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
      
      'format' => '?paged=%#%',
      
      'current' => max( 1, get_query_var('paged') ),
      
      'total' => $wp_query->max_num_pages
      

      ));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-25
        • 2019-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多