wordpress ajax return 0

时间:2016-03-02 作者:Nicola Bertelloni

我想成立一个小组,这个enter image description here

顶部的菜单调用不同的ajax函数以显示拇指。单击拇指可以在面板的最后一个框中查看详细信息。

我有这个php 功能

function GetPostPartner(){
$catPartner = "loop_pb_partner";
get_template_part($catPartner);
wp_die();
}
add_action(\'wp_ajax_nopriv_GetPostPartner\', \'GetPostPartner\');

function GetPostEnte(){
$catEnte = "loop_pb_ente";
get_template_part($catEnte);
wp_die();
}
add_action(\'wp_ajax_nopriv_GetPostEnte\', \'GetPostEnte\');

function GetPostColl(){
$catColl = "loop_pb_coll";
get_template_part($catColl);
wp_die();
}
add_action(\'wp_ajax_nopriv_GetPostColl\', \'GetPostColl\');

function GetPostMedia(){
$catMedia = "loop_pb_media";
get_template_part($catMedia);
wp_die();
}
add_action(\'wp_ajax_nopriv_GetPostMedia\', \'GetPostMedia\');

function GetPostDetails(){
$pb_details = $_POST[\'postURL\'];
get_template_part($pb_details);
wp_die();
}
add_action(\'wp_ajax_nopriv_GetPostDetails\', \'GetPostDetails\');
这些都是由这些ajax函数调用的

$(document).delegate(\'h4.homus-partners-global-ajax[data-pb-               cat*=pb_partner]\', \'click\', function(event) {
event.preventDefault();
var pb_cat = "pb_partner";
var data = {
    \'action\': \'GetPostPartner\',
    catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
    $( \'ul.homus-partners-section-slide\' ).html(response);
});

});

$(document).delegate(\'h4.homus-partners-global-ajax[data-pb-cat*=pb_ente]\', \'click\', function(event) {
event.preventDefault();
var pb_cat = "pb_ente";
var data = {
    \'action\': \'GetPostEnte\',
    catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
    $( \'ul.homus-partners-section-slide\' ).html(response);
});

});

$(document).delegate(\'h4.homus-partners-global-ajax[data-pb-cat*=pb_coll]\', \'click\', function(event) {
event.preventDefault();
var pb_cat = "pb_coll";
var data = {
    \'action\': \'GetPostColl\',
    catURL : "loop_"+ pb_cat,
};

$.post(ajaxURL, data, function(response) {
    $( \'ul.homus-partners-section-slide\' ).html(response);
});

});

$(document).delegate(\'h4.homus-partners-global-ajax[data-pb-cat*=pb_media]\', \'click\', function(event) {
event.preventDefault();
var pb_cat = "pb_media";
var data = {
    \'action\': \'GetPostMedia\',
    catURL : "loop_"+ pb_cat,
};
$.post(ajaxURL, data, function(response) {
    $( \'ul.homus-partners-section-slide\' ).html(response);
});

});

$(document).delegate(\'li.homus-partners-section-single\', \'click\',                         function(event) {
  event.preventDefault();
  var pb_post_id = $(this).data(\'post-id\');
  var data = {
    \'action\': \'GetPostDetails\',
    postURL : "single_pb_post_details",
    post_id: pb_post_id
  };
  $.post(ajaxURL, data, function(response) {
      $( \'.homus-partners-detalis\' ).html(response);
      console.log(pb_post_id);
      console.log(data.postURL);
      console.log(response);
  });

});
即使上面最后一个ajax调用的控制台返回正确的postid,我的响应始终为0。你可以在这份回购协议中找到整个项目https://github.com/wanbinkimoon/homus-web.git

1 个回复
最合适的回答,由SO网友:Devendra Sharma 整理而成

因此,如果您想让它在前端为访客用户和登录用户启动,可以执行以下操作:

add_action( \'wp_ajax_my_action\', \'my_action_callback\' );
add_action( \'wp_ajax_nopriv_my_action\', \'my_action_callback\' );

Example

add_action( \'wp_ajax_GetPostMedia\', \'GetPostMedia\' );
add_action( \'wp_ajax_nopriv_GetPostMedia\', \'GetPostMedia\' );

相关推荐

Is_admin()和is_blog_admin()之间的实际区别是什么?

我可以看出这两个函数几乎是相同的。唯一的区别在于一行:@is\\u admin()return $GLOBALS[\'current_screen\']->in_admin();@is\\u blog\\u admin()return $GLOBALS[\'current_screen\']->in_admin( \'site\' );这背后的实际目的/原因是什么?或者我在哪里可以读到更多关于它的信息?我看得出来is_blog_admin() \'“出生”的时间要晚得多,因为它将“site”参