wp paginate_links和查询变量无法运行Wordpress

时间:2016-12-30 14:01:47

标签: wordpress wordpress-theming query-string paginate

我正在尝试使用插件集成自定义查询来对帖子进行排序。

这是代码。

的index.php

$view =$_GET["sort"];
if($view == "views"){
  if ( have_posts() ) :
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $args = array('post_type' => 'post', 'meta_key' => '_kksr_avg', 'orderby' => 'meta_value', 'order' => 'desc', 'paged' => $paged, 'posts_per_page' => 5);
    $query = new WP_Query($args);

    while ( $query->have_posts() ) : $query->the_post();
      // code for display posts
    endwhile;

    if (function_exists(custom_pagination)) :
      custom_pagination($query->max_num_pages,"",$paged);
      wp_reset_postdata();
    endif;
  endif;
} 

这是 functions.php 代码。

function custom_pagination($numpages = '', $pagerange = '', $paged='') {
  if (empty($pagerange)) {
    $pagerange = 2;
  }

  global $paged;
  if (empty($paged)) {
    $paged = 1;
  }
  if ($numpages == '') {
    global $wp_query;
    $numpages = $wp_query->max_num_pages;
    if(!$numpages) {
      $numpages = 1;
    }
  }
  $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'        => false,
    'add_fragment'    => ''
  );
  $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>";
  }
}

问题在于分页链接。与查询字符串冲突。

  

domain.com/?sort=views - &gt;第一页

     

domain.com/?sort=viewspage/2 - &gt; for sec。页

这不起作用。

有没有解决方法可以解决这个问题?

1 个答案:

答案 0 :(得分:0)

你不需要在下面使用任何插件进行排序我使用isset发布了一个简单的代码

<?php 
$orderby = 'date';
$order = isset( $_REQUEST['sort'] ) ? trim( $_REQUEST['sort'] ) : null;
if(isset($_GET['sort']) && !empty($_GET['sort'])) {
    switch($order)
    {
        case 'views' :  $meta_key = '_kksr_avg';
                                $orderby = 'meta_value_num';
            break;

        case 'newest' : $meta_key = 'post_views_count'; 
                                  $orderby = 'date';
            break;
    }
}
$args = array(
'posts_per_page' => 20,
'meta_key' => $meta_key,
'orderby' => $orderby,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ), );
query_posts($args);  ?> 
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>

 // code for display posts

<?php 

endif; 

endwhile; 

else : 

endif;

custom_pagination($query->max_num_pages,"",$paged);

wp_reset_query();

?>