私はワードプレスのテーマに取り組んでいます、あなたは見ることができます:greenzoner.pl。私がやりたいのは、すべての投稿のユーザビリティを中央のdivに表示することです。つまり、単一の投稿、コメント、添付の写真などです。2つのサイドdivはリロードしないでください。私は、使用しようとしている方法(おそらくIFRAME?)の代替案を受け入れています。
単一の投稿をロードするには、jquery .load()を使用しました
$.ajaxSetup({ cache: false });
$(".openpost, .comments-link").click(function() {
var post_url = $(this).attr("href");
$(".container").load(post_url);
return false;
});
$(".closepost").click(function() {
$(".container").load(index.php .postlist);
return false;
});
私は大きな問題に遭遇しました-ajaxコメントを使用できません(私は多くのプラグイン、チュートリアルなどを試しましたが、何も機能しません)-基本的に返信を投稿すると、たとえばにリダイレクトされます。http://greenzoner.pl/?p=32#comment-26。コメントの私の現在のjsは次のとおりです。
var commentform = $('#commentform'); // find the comment form
commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors
var statusdiv = $('#comment-status'); // define the infopanel
commentform.submit(function(){
//serialize and store form data in a variable
var formdata = commentform.serialize();
//Add a status message
statusdiv.html('<p>Processing...</p>');
//Extract action URL from commentform
var formurl = commentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown) {
statusdiv.html('<p class="wdpajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
},
success: function(data, textStatus) {
if (data == "success")
statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
else
statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
commentform.find('textarea[name=comment]').val('');
}
});
});
およびphp:
add_action('comment_post', 'ajaxify_comments',20, 2);
function ajaxify_comments($comment_ID, $comment_status) {
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
//If AJAX Request Then
switch($comment_status){
case '0':
//notify moderator of unapproved comment
wp_notify_moderator($comment_ID);
case '1': //Approved comment
echo "success";
$commentdata = &get_comment($comment_ID, ARRAY_A);
$post = &get_post($commentdata['comment_post_ID']);
wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
break;
default:
echo "error";
}
exit;
}
}
この方法を使用する際のその他の問題は、ポストリンクです。