简体   繁体   English

从wordpress函数获取空的AJAX响应

[英]Getting an empty AJAX response from wordpress function

I'm getting an empty AJAX response from my function in my functions.php . 在我的functions.php中,我的functions.php收到了空的AJAX响应。 Here's my function that processes the AJAX request. 这是我处理AJAX请求的函数。

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: 这是AJAX调用:

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. 现在,如果我删除了wordpress循环并仅回显了$ year和$ month,它将附加到容器中并且可以正常工作。 When I add the loop though I get an empty response and nothing is displayed. 当我添加循环时,尽管得到的响应为空,但未显示任何内容。

Am I handling the response properly? 我是否正确处理了回复?

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 因此,这将查找content-format.php文件,在这种情况下, formats

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. 因此,根据帖子的formatWordPress将在模板的根文件夹中查找文件。 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). 因此,请确保您具有相应的文件,例如content-aside.phpcontent-chat.php等(如果您具有这些类型的帖子)。 For, default/normal post, get_post_format() returns false , so try this instead 对于默认/普通帖子, get_post_format()返回false ,因此请尝试使用此方法

$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. 最后,确保您有根据查询的帖子,只是var_dump($loop) ,看看您是否真的得到了任何结果,或者它是否为空。

If you don't have any post-format specific posts then, you may use, 如果您没有任何关于post-format帖子,则可以使用,

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' ); 并将您的文件名更改为content-loop.php ,因此当您调用get_template_part( 'content', 'loop' );时, WordPress将查找/包含content-loop.phpcontent.php文件get_template_part( 'content', 'loop' ); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM