我有一个WordPress多站点设置。除了我的ajax调用返回500s之外,一切正常,这是我转换为多站点时非常确定的事情。
拨打电话的Ajax代码片段:
function ajaxShowComments(post,numberToLoad){
$.ajax({
url: eventComments.commenturl,
type: 'post',
data:{
'postId': post,
'comment-offset': offset,
'count': numberToLoad
},
success: function(result){
var arr = JSON.parse(result);
if( arr.length == 0 ) {
$('.readmore').css('display', 'none');
if( arr.length == 0 && offset == 0 )
$('.latestShows').append('<div id="nocomment"> No one has left a comment for this show yet! </div> <button class="first-comment-btn">Be The First!</button>')
$('.first-comment-btn').click(function () {
showModal();
});
}
arr.forEach(function (item) {
$('.readmore').before('<div class="lazy-comment">' + item + '</div>');
});
offset += numberToLoad;
}
});
}
我的主题的片段functions.php,它创建了Ajax调用的URL:
function pebl_culture_assets() {
// Enqueue/register other styles/scripts etc...
wp_localize_script('pebl-main', 'eventComments', array(
'commenturl' => get_template_directory_uri().'/getcomments.php')
);
}
add_action( 'wp_enqueue_scripts', 'pebl_culture_assets' );
这很好用,html头中的脚本看起来像这样:
//script tags omitted
var eventComments = {"commenturl":"http:\/\/peblarts.com\/{{SUBSITE NAME}}\/wp-content\/themes\/peblculture\/getcomments.php"};
到目前为止,脚本加载了传递给AJAX调用的URL(post,offset,numberToLoad都在js中定义)。该调用肯定是针对getcomments.php进行的,如下所示:
<?php
require_once("../../../wp-load.php");
$cmts = get_comments( array( 'post_id' => $_POST['postId'] , 'status' => 'approve') );
$comments = [];
$count = $_POST['count'];
if( !empty($cmts) ) {
for ($i = $_POST['comment-offset']; $i < $_POST['comment-offset'] + $count; $i++ ) {
if( !empty($cmts[$i]) )
array_push($comments, $cmts[$i]->comment_content);
}
echo json_encode($comments);
}else{
echo json_encode(array());
}
但是在尝试获取评论时似乎打破了,非常烦人,我尝试过编辑文件权限,尝试更改URL以明确指向我所知道的getcomments.php所处的位置,但这并不能解决问题。一整天都有500多人。