wp paginate_links和查询变量不起作用Wordpress [英] wp paginate_links and query vars not working Wordpress
本文介绍了wp paginate_links和查询变量不起作用Wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将自定义查询与插件集成在一起以对帖子进行排序.
I am trying to make custom query integrated with a plugin to sort posts.
这是代码.
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>";
}
}
问题来自分页链接.与查询字符串冲突.
The problem comes with the paginate links. conflict with the query string.
domain.com/?sort=views->第一页
domain.com/?sort=views -> for first page
domain.com/?sort=viewspage/2->秒钟.页面
domain.com/?sort=viewspage/2 ->for sec. page
这不起作用.
有解决此问题的解决方案吗?
Is there any solution to fix this?
推荐答案
您无需在下面使用任何插件进行排序,即时消息使用isset向您发布简单的代码
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();
?>
这篇关于wp paginate_links和查询变量不起作用Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文