我已经编写了一个与Beaver Builder一起使用的自定义日历模块。
我想从“事件”的自定义post_type中获取帖子,以填充日历。
在我的beaver构建器模块中,我有以下内容:
wp_enqueue_script( 'axios', 'https://unpkg.com/axios/dist/axios.min.js');
wp_enqueue_script( 'qs', 'https://unpkg.com/qs/dist/qs.js');
function get_ajax_event_calendar_posts() {
// Query Arguments
$args = array(
'post_type' => array('event'),
'post_status' => array('publish'),
'posts_per_page' => 40,
'nopaging' => true,
'order' => 'DESC',
'orderby' => 'date',
'cat' => 1,
);
// The Query
$ajaxposts = get_posts( $args ); // changed to get_posts from wp_query, because `get_posts` returns an array
echo json_encode( $ajaxposts );
wp_die(); // this is required to terminate immediately and return a proper response
}
// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_event_calendar_posts','get_ajax_event_calendar_posts');
add_action('wp_ajax_nopriv_get_ajax_event_calendar_posts', 'get_ajax_event_calendar_posts');
并且在我的模块JavaScript(frontend.php)中,我有:
var data = { action: "get_ajax_event_calendar_posts" };
axios.post("<?php echo admin_url('admin-ajax.php');?>", Qs.stringify(data))
.then(function(response) {
console.log(JSON.stringify(response.data));
})
.catch(function(error) {
console.log(error);
});
我在“活动”中发表了一篇文章。在浏览器中,我通过访问xxx.flywheelsites.com/wp-admin/admin-ajax.php?action=get_ajax_event_calendar_posts
测试端点并收到一个空数组[]
。我希望收到一个事件。我看过管理员,可以看到该帖子。
单击模块中的按钮以发出Ajax请求时,我收到400 error
[检查日志,我收到以下错误消息:
(MISSING)127.0.0.1 - 04/Jan/2020:16:41:40 +0000 "POST /.wordpress/wp-admin/admin-ajax.php" 400 /www/.wordpress/wp-admin/admin-ajax.php 64263000 4103576 709.963 4096 35.21%!
此错误消息并没有真正告诉我任何事情。我也将axios方法更改为使用GET
-但是,我收到了相同的错误。
关于如何调试的任何建议都会很有帮助。谢谢!
Wordpress,Astra主题,Beaver Builder,由飞轮托管。
**编辑**
为了测试,在我的get_ajax_event_calendar_posts
方法中,我正在呼出'hello world'
function get_ajax_event_calendar_posts() {
// Query Arguments
$args = array(
'post_type' => array('event'),
'post_status' => array('publish'),
'posts_per_page' => 40,
'nopaging' => true,
'order' => 'DESC',
'orderby' => 'date',
'cat' => 1,
);
// The Query
$ajaxposts = get_posts( $args ); // changed to get_posts from wp_query, because `get_posts` returns an array
echo 'hello world'; //json_encode( $ajaxposts );
wp_die(); // this is required to terminate immediately and return a proper response
}
我在浏览器中访问url并看到'hello world'-看来查询数据可能存在问题吗?日志不包含任何有用的信息。但是,由此我可以看到路线并从中得到响应。
**编辑**
我现在可以直接点击url来查看帖子。似乎是缓存问题。...我更新了JS,但旧文件仍然存在。我希望这只是一个缓存问题...
使用Beaver Builder,我必须清除Beaver Builders缓存。要清除它,请进入settings -> beaver-builder -> tools
并单击清除缓存按钮。