dtpwra8456 2013-09-16 20:32
浏览 41
已采纳

从wordpress函数获取空的AJAX响应

I'm getting an empty AJAX response from my function in my functions.php. Here's my function that processes the AJAX request.

function dynamic_date() {
check_ajax_referer('dynamic_date_nonce');
$fdate = $_GET['my_date'];
$date[] = explode("-",$fdate);
$year = $date[0];
$month = $date[1];
$args = array('year' => $year,'monthnum' => $month);
$loop = new WP_Query($args);
if($loop->have_posts()) { 
while($loop->have_posts()) { 
the_post();
echo get_template_part( 'content', get_post_format() );
} 
} wp_reset_query();
die('');
}

And this is the AJAX call:

function _do_ajax(obj) {
    var element = $(obj); //our link object
    var url = wpAjax.unserialize(element.attr('href'));
    var s = {};
    s.response = 'ajax-response';
    s.type = "GET";
    s.url = sortbydate.ajax_url;
    s.dataType = "HTML";
    s.data = $.extend(s.data, { action: url.action, _ajax_nonce: url._wpnonce, my_date: url.my_date });
    s.global = false;
    s.timeout = 30000;
    s.success = function(data) {
        $("body.blog #content").fadeIn(500).append(data);
        alert(data);            
    } //End success
    s.error = function(r) {
        alert("Epic Fail!");    
    }
    $.ajax(s);
} //end _do_ajax
$.get_my_comments.init();
});

Now if I remove the wordpress loop and just echo $year and $month it appends to the container and works fine. When I add the loop though I get an empty response and nothing is displayed.

Am I handling the response properly?

  • 写回答

1条回答 默认 最新

  • duangu4980 2013-09-16 21:01
    关注

    In your code you have

    get_template_part( 'content', get_post_format() );
    

    So, this will look for content-format.php file and in this case formats are

    aside, chat, gallery, link, image, quote, status, video, audio
    

    So, depending on the format of the post, WordPress will look for a file in template's root folder. So, make sure, you have corresponding files, such as, content-aside.php, content-chat.php and so on (if you have these types of posts). For, default/normal post, get_post_format() returns false, so try this instead

    $format = get_post_format();
    if ( false === $format ) {
        $format = 'standard';
    }
    get_template_part( 'content', $format );
    

    Finally, make sure that, you have posts according to your query, just var_dump($loop) and see if you really get any result or is it empty at all.

    If you don't have any post-format specific posts then, you may use,

    get_template_part( 'content', 'loop' );
    

    and change your file name to content-loop.php, so WordPress will look for/include content-loop.php or content.php file when you call get_template_part( 'content', 'loop' );.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line