AJAX和Wordpress - 如何从div ID加载内容?

时间:2013-02-20 14:28:57

标签: php ajax wordpress jquery

我无法从single.php获取div(宽度特定ID)的内容到带有ajax的index.php。 现在我有了这段代码并且什么也没有返回 - “0”。

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;    
}

如何转移内容?感谢

1 个答案:

答案 0 :(得分:0)

您的loadContent()函数的成功处理程序绝对没有。因此,无论您的PHP脚本发回什么,都会被丢弃。

你需要这样的东西:

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

另请注意,isarray($output)永远不会有效。 json_encode()返回 STRING ...永远不会是数组。