2013-02-20 98 views
0

我無法從single.php中將div的內容(寬度特定ID)轉換爲帶有ajax的index.php。 現在我有這個代碼並返回「0」。AJAX和Wordpress之間的加載函數不起作用

$("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'); 
}); 
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) {} 
    }); 
} 

和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'。 – marteljn 2013-02-20 14:07:45

+0

我也有這部分.. – 2013-02-20 14:11:35

回答

0

我有一部分太..

$("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); 
});