我有一个js
文件如下:
var category = [];
var genre = [];
var type = [];
var mainContent = jQuery(\'#content\');
var siteURL ="http://" + top.location.host.toString();
var URL = siteURL + "/?category="+ category +"&genre=" + genre +"&type="+ type +" #content";
jQuery(\'.ajax-cb\').each(function() {
jQuery(this).click(function() {
mainContent.load(URL,function(){
mainContent.animate({opacity: \'1\'});
});
});
});
它确实加载
GET[]
用于更改循环的参数,
在主题的function.php
我有:
function pre_selected_results() {
$taxquery = array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => $_GET[\'category\'],
\'operator\'=>\'IN\'
),
array(
\'taxonomy\' => \'genres\',
\'field\' => \'slug\',
\'terms\' => $_GET[\'genre\'],
\'operator\'=>\'IN\'
),
array(
\'taxonomy\' => \'types\',
\'field\' => \'slug\',
\'terms\' => $_GET[\'type\'],
\'operator\'=>\'IN\'
)
);
$stack[] = "dog";
$the_query = new WP_Query($myquery);
while ($the_query->have_posts()) :
//the loop
//I then collect info from the loop displayed
$args = array(\'orderby\' => \'name\', \'order\' => \'ASC\', \'fields\' => \'slugs\');
$results = wp_get_post_terms(get_the_ID(),\'category\',$args);
foreach ($results as $result){
array_push($stack, $result);
}
endwhile;
/*And I use wp_localize_script(); to send $stack back to js*/
wp_enqueue_script( \'feedback\' );
wp_localize_script( \'feedback\', \'jsdata\', $stack);
}; //end of pre_selected_results
在我的主页上,我呼叫
pre_selected_results();
循环显示良好并刷新,
但是$stack
仅在编辑URL时刷新GET[]
在浏览器中手动,
否则,它只会返回“狗”,有什么明显的东西是我遗漏的吗?
SO网友:Tom J Nowell
您应该使用WP AJAX API,而不是在您的主模板中重新创建它们,例如:
功能。php:
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( \'my-ajax-request\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
add_action(\'wp_ajax_my_action\', \'my_action_callback\');
add_action(\'wp_ajax_nopriv_my_action\', \'my_action_callback\');
function my_action_callback() {
$content = \'something to send back to the browser\';
echo $content;
die(); // this is required to return a proper result
}
Javascript:
jQuery.post(
// see tip #1 for how we declare global javascript variables
MyAjax.ajaxurl,
{
// here we declare the parameters to send along with the request
// this means the following action hooks will be fired:
// wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
action : \'myajax-submit\',
// other parameters can be added along with "action"
postID : MyAjax.postID
},
function( response ) {
alert( response ); //
}
);
使用它,您可以绕过已损坏的代码,完全依赖标准化的API
您可以在此处阅读更多信息:
http://codex.wordpress.org/AJAX_in_Plugins
它也适用于主题