我知道这是一个常见的问题,但我找不到解决办法。
我在用户点击复选框时进行了ajax过滤。在用户单击分页之前,一切都正常。当然,我们现在使用的是管理ajax。php页面和分页链接不工作。
这是代码。
HTML复选框:
<input type="checkbox" id="telegram_chk" checked>
<input type="checkbox" id="twitter_chk" checked>
<input type="checkbox" id="eth_chk" checked>
<input type="checkbox" id="xml_chk" checked>
JavaScript:
jQuery(\'#content\').on(\'change\', \'#telegram_chk, #twitter_chk, #eth_chk, #xml_chk\', function(e) {
e.preventDefault();
if (jQuery(\'#telegram_chk\').is(":checked"))
var telegram="";
else
var telegram="facebook";
if (jQuery(\'#twitter_chk\').is(":checked"))
var twitter="";
else
var twitter="twitter";
if (jQuery(\'#eth_chk\').is(":checked"))
var eth="";
else
var eth="ethereum";
if (jQuery(\'#xml_chk\').is(":checked"))
var xml="";
else
var xml="stellar";
var checkboxes = [telegram, twitter, eth, xml]
jQuery.ajax({
url : rml_obj.ajax_url,
type : \'post\',
data : {
action : \'test_function\',
security : rml_obj.check_nonce,
test_data : checkboxes
},
success : function( response ) {
jQuery(\'#result\').html(response);
}
});
});
和功能。php:
function test_function() {
check_ajax_referer( \'rml-nonce\', \'security\' );
$test_data = $_POST[\'test_data\'];
$telegram= $test_data[0];
$twitter = $test_data[1];
$ethereum= $test_data[2];
$stellar = $test_data[3];
$tax_query_args[] = array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'filters\',
\'field\' => \'slug\',
\'terms\' => array ($telegram, $twitter),
\'relation\' => \'AND\',
\'operator\' => \'NOT IN\'
),
array(
\'taxonomy\' => \'platform\',
\'field\' => \'slug\',
\'terms\' => array ($ethereum, $stellar),
\'relation\' => \'AND\',
\'operator\' => \'NOT IN\'
),
);
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array(
\'post_type\' => \'post\' ,
\'orderby\' => \'date\',
\'order\' => \'DESC\',
\'posts_per_page\' => 15,
\'cat\' => 4,
\'paged\' => $paged,
\'tax_query\' => $tax_query_args
);
$q = new WP_Query($args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();?>
?>
<div>
Display results
</div>
<?php
}
$pages =$q->max_num_pages;
$range = 2;
$showitems = ($range * 2);
if(1 != $pages) {
echo "<div class=\'pagination\'>";
if($paged > 1 && $showitems < $pages) echo "<a class=\'inactive\' href=\'".get_pagenum_link($paged - 1)."\'>‹</a>";
if ($pages > $showitems) {
if($showitems == $paged) {echo "<a class=\'inactive\' href=\'".get_pagenum_link(1)."\'>1</a>";}
if($paged > $showitems) {echo "<a class=\'inactive\' href=\'".get_pagenum_link(1)."\'>1</a>"; echo \'<span class="inactive">..</span>\';}
}
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? "<span class=\'current\'>".$i."</span>":"<a href=\'".get_pagenum_link($i)."\' class=\'inactive\' >".$i."</a>";
}
}
if ($pages > $showitems) {
if ( ($paged < $pages) && ($pages-$paged >= $showitems) ) {
echo \'<span class="inactive">..</span>\'; echo "<a class=\'inactive\' href=\'".get_pagenum_link($pages)."\'>".$pages."</a>";
}
if ( ($paged < $pages) && ($pages-$paged == $showitems-1) ) {
echo "<a class=\'inactive\' href=\'".get_pagenum_link($pages)."\'>".$pages."</a>";}
}
if ($paged < $pages && $showitems < $pages) {
echo "<a class=\'inactive\' href=\'".get_pagenum_link($paged + 1)."\'>›</a>";
echo "</div>\\n";
}
}
}
else {
echo "Sorry, we have not found any posts.";
}
die();
}
add_action(\'wp_ajax_test_function\', \'test_function\');
add_action(\'wp_ajax_nopriv_test_function\', \'test_function\');
如果能帮我解决这个问题,我将不胜感激。
非常感谢。
最合适的回答,由SO网友:Sally CJ 整理而成
我会这样做:(见// comment here
在代码中)
// Wrap the AJAX call to `test_function` in a `function`.
function ajaxTestFunction( page_num ) {
if (jQuery(\'#telegram_chk\').is(":checked"))
var telegram="";
else
var telegram="facebook";
if (jQuery(\'#twitter_chk\').is(":checked"))
var twitter="";
else
var twitter="twitter";
if (jQuery(\'#eth_chk\').is(":checked"))
var eth="";
else
var eth="ethereum";
if (jQuery(\'#xml_chk\').is(":checked"))
var xml="";
else
var xml="stellar";
var checkboxes = [telegram, twitter, eth, xml]
jQuery.ajax({
url : rml_obj.ajax_url,
type : \'post\',
data : {
action : \'test_function\',
security : rml_obj.check_nonce,
test_data : checkboxes,
paged: page_num || 1
},
success : function( response ) {
jQuery(\'#result\').html(response);
}
});
}
// In the `change` callback, we call `ajaxTestFunction()`.
jQuery( \'#content\' ).on( \'change\', \'#telegram_chk, #twitter_chk, #eth_chk, #xml_chk\', function( e ) {
// Starts at page #1
ajaxTestFunction();
});
// And add a listener/callback for the pagination clicks.
jQuery( \'#result\' ).on( \'click\', \'.pagination a\', function( e ){
e.preventDefault();
var paged = /[\\?&]paged=(\\d+)/.test( this.href ) && RegExp.$1;
ajaxTestFunction( paged );
});
在PHP中
test_function()
功能,捕获
paged
像这样:
$paged = $_POST[\'paged\'];
。。而不是:
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
Additional Note
你为什么不利用
paginate_links()
WordPress中的函数?=)
从此行开始的代码:
$pages =$q->max_num_pages;
。。直到
}
以上此:
}
else {
echo "Sorry, we have not found any posts.";
}
。。可替换为:
$links = paginate_links([
\'base\' => \'%_%\',
\'format\' => \'?paged=%#%\',
\'total\' => $q->max_num_pages,
\'current\' => $paged,
]);
if ( $links ) {
echo \'<div class="pagination">\';
echo $links;
echo \'</div>\';
}
请参见
https://codex.wordpress.org/Function_Reference/paginate_links 和/或
https://developer.wordpress.org/reference/functions/paginate_links/.