0
我無法從single.php獲取div(寬度特定ID)的內容到帶有ajax的index.php。 現在我有這個代碼並且什麼都不返回 - 「0」。AJAX和Wordpress - 如何從div ID加載內容?
function loadcontent(type,id,slug, action) {
jQuery.ajax({
type:'POST',
url:srvars.ajaxurl,
data: {
action:'sr_get_content',
id:id,
type:type
},
success: function(response) {
if ($(window).width() > 768) {
$('#content .content-inner').hide();
$('#content .content-inner').html(response);
initialise('#content');
}
var checkwidth = $('.maincontent').width();
$('#loading').delay(1000).fadeOut(fadeInOutSpeed, function(){
if ($(window).width() > 768) {
if (checkwidth > 0) {
reorganizeIsotope('.masonry');
$('#content .content-inner').fadeIn(fadeInOutSpeed, function() { resize_jplayer(); reorganizeIsotope('.masonry'); });
} else {
$('.mainside').animate({'width':srvars.asidewidth+'%'}, animationSpeed, $easingType);
$('.mainside-bg').animate({'width':srvars.asidewidth+'%'}, animationSpeed, $easingType);
$('.maincontent').animate({'width':srvars.contentwidth+'%'}, animationSpeed, $easingType, function() {
reorganizeIsotope('.masonry');
$('#content .content-inner').fadeIn(fadeInOutSpeed, function() { resize_jplayer(); reorganizeIsotope('.masonry'); });
});
}
} else {
$('#content .content-inner').hide();
$('#content .content-inner').html(response);
initialise('#content');
if ($(window).width() <= 480) { var topscroll = jQuery('header').height(); } else { var topscroll = 0; }
$('html, body').animate({scrollTop: topscroll+'px'}, animationSpeed, $easingType);
reorganizeIsotope('.masonry');
$('#content .content-inner').slideDown(fadeInOutSpeed, $easingType, function() {
resize_jplayer();
reorganizeIsotope('.masonry');
});
}
});
}
});
}
$("body").on("click", 'a.loadcontent', function() {
var href = $(this).attr('href');
var type = $(this).data('type');
var id = $(this).data('id');
var slug = $(this).data('slug');
if(window.location.hash.substr(1) == slug) {
$('html, body').animate({scrollTop: 0}, animationSpeed, $easingType);
} else {
window.location.hash = slug; // set the hash
//history.pushState({page:href}, href, href);
loadcontent(type,id,slug,false);
}
return(false);
});
和functions.php中
add_action('wp_ajax_nopriv_sr_get_content', 'loadAjaxPosts');
add_action('wp_ajax_sr_get_content', 'loadAjaxPosts');
function loadAjaxPosts() {
switch($_REQUEST['type']) {
case 'portfolio':
$output = sf_portfolio_items($_REQUEST['cat'], $_REQUEST['count'], $_REQUEST['page'], true);
break;
case 'blog':
$output = sf_blog_items($_REQUEST['cat'], $_REQUEST['count'], $_REQUEST['page'], true);
break;
default:
$output = 'Incorrect type specified, please contact support.';
break;
}
$output=json_encode($output);
if(is_array($output)) {
print_r($output);
} else {
echo $output;
}
die;
}
如何將內容傳送?謝謝
已更新我的代碼 – 2013-02-20 14:45:31