简体   繁体   English

WordPress的:jQuery.load一些PHP文件

[英]Wordpress: jQuery.load some php file

I want to load some dynamic data with jQuery after document load. 我想在文档加载后使用jQuery加载一些动态数据。 For example, let it be tag cloud: 例如,将其设为标签云:

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#sidebar-tags').load('<?php echo bloginfo('template_url'); ?>/tag_cloud.php');
});
</script>

And in tag_cloud.php something like this: 在tag_cloud.php中,如下所示:

<? wp_tag_cloud(''); ?>

And ofcoz it doesn't work, "Internal server error". 而且ofcoz不起作用,“内部服务器错误”。 How can i make this idea work? 我如何使这个想法可行?

Solved: 解决了:

Followed Plamen Nikolov's link, came up with this: 按照Plamen Nikolov的链接,提出了以下内容:

<script type="text/javascript">
jQuery(document).ready(function() {
jQuery.ajax({
         type : "post",
         dataType : "json",
         url : "/wp-admin/admin-ajax.php",
         data : ({action: "get_tag_cloud"}),
         success: function(response) {
            alert(response);}
         })
});
</script>

and in functions.php: 并在functions.php中:

add_action('wp_ajax_get_tag_cloud', 'get_tag_cloud');
add_action('wp_ajax_nopriv_get_tag_cloud', 'get_tag_cloud');
function get_tag_cloud() {
    echo json_encode(wp_tag_cloud('echo=0'));
    die();
}

It almost works: if I check it with Firebug, I can see the response, but the alert doesn't pop up. 几乎可以正常工作:如果我用Firebug进行检查,可以看到响应,但不会弹出警报。

To send a XHR request (Ajax) you must stick to the recommended way, using the proper hooks: 要发送XHR请求(Ajax),必须使用适当的钩子坚持建议的方式:

See the examples here: WordPress Ajax hooks 在此处查看示例: WordPress Ajax挂钩

The code functionality in tag_cloud.php is wrong and it responses internal server error. tag_cloud.php中的代码功能错误,并且它会响应内部服务器错误。 In order to develop wordpress widget you can have a look at following tutorial 为了开发wordpress小部件,您可以看一下以下教程

http://wp.tutsplus.com/tutorials/plugins/creating-a-wordpress-network-widget http://wp.tutsplus.com/tutorials/plugins/creating-a-wordpress-network-widget

Also be sure that, you are calling correct url in jquery load function. 另外请确保您在jquery加载函数中调用了正确的url。

$("#sidebar").html($.get('<?php echo bloginfo("template_url"); ?>/tag_cloud.php'); 

You have to either escape single-quotes around template_url or use double-quotes. 您必须转义template_url周围的单引号或使用双引号。 Read your server logs for further debugging. 阅读服务器日志以进行进一步调试。

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

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