在 WordPress 的前端调用 Ajax 无法正常工作,我只想从基于 id 的 db 中获取数据,这是我迄今为止尝试过的。
//Frontend view
<a href="javascript:void(0)" onclick="getDetails(<?=$index;?>)" >
<?php echo $value ?>
</a>
//Functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/assets/js/ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
add_action('wp_ajax_nopriv_more_posts', 'get_more_posts');
function get_more_posts(){
// How to get id here to query for the database
echo "Hello World";
exit();
}
// Ajax scripts File
function getDetails(id)
{
jQuery.ajax({
url: more_posts.ajax_url,
type: "GET",
data: {
action: 'more_posts'
},
dataType: "html",
success: function(response){
alert(response);
}
});
}
我收到此错误
参考错误 more_posts 未定义