簡體   English   中英

AJAX和Wordpress-如何從div ID加載內容?

[英]AJAX and Wordpress - how to load content from div ID?

我無法使用ajax將div(具有特定ID的寬度)的內容從single.php轉換為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;    
}

如何傳輸內容? 謝謝

您的loadContent()函數的成功處理程序絕對不執行任何操作。 因此,無論您的php腳本發回什么,都將被丟棄。

您將需要以下內容:

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

還要注意,您的isarray($output)永遠不會工作。 json_encode()返回STRING ...永遠不會返回數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM