wp paginate_links and query vars not working Wordpress

喜你入骨 提交于 2019-12-25 08:17:22

问题


I am trying to make custom query integrated with a plugin to sort posts.

Here's the code.

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;
} 

And here is the functions.php code.

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>";
  }
}

The problem comes with the paginate links. conflict with the query string.

domain.com/?sort=views -> for first page

domain.com/?sort=viewspage/2 ->for sec. page

This is not working.

Is there any solution to fix this?


回答1:


You dont need to use any plugin for sorting below Im posting you a simple code using 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();

?>


来源:https://stackoverflow.com/questions/41397510/wp-paginate-links-and-query-vars-not-working-wordpress

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!