1:admin-ajax.php
前端页面:
<script type="text/javascript"> jQuery.ajax({ type: 'POST', url: "<?php echo admin_url( 'admin-ajax.php' );?>", data: { action: 'myajax_submit' }, success: function(res) { console.log(JSON.stringify("--"+res)); } }); </script>
后端functions.php里面接受请求:
add_action( 'wp_ajax_nopriv_myajax_submit', 'myajax_submit' ); add_action( 'wp_ajax_myajax_submit', 'myajax_submit' ); function myajax_submit() { $response = json_encode( array( 'success' => true ) ); header( "Content-Type: application/json" ); echo $response; // 这个很关键啊有木有: 别忘记 "exit" exit; }
注意:前端action名字要和后端wp_ajax_nopriv_XXX名字后缀对应起来
2.WP_Query()
get_query_var()
wp_reset_postdata()