2013-02-20 34 views
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;  
} 

如何将内容传送?谢谢

回答

0

你的loadContent()函数的成功处理程序绝对没有什么。因此,无论你的PHP脚本发回是简单地扔掉。

你会需要像:

success: function(response) { 
     $('#whatever').html(response); 
    } 

另外请注意,您的isarray($output)不会有任何效果。 json_encode()返回一个STRING ...从来没有一个数组。

+0

已更新我的代码 – 2013-02-20 14:45:31