我尝试从wordpress网站上的外部文件运行php脚本。
示例:
jQuery的:
jQuery('.button').click(function(){
jQuery(this).parent('.entry_card').append('<div class="static"></div>');
jQuery(this).parent('.entry_card').children('.static').load("insert.php");
});
insert.php:
<?php
echo date("d.m.Y h:i:s"); //for testing
echo the_title(); //wordpress post title
if( get_field('xxx') ) { insert_cform('1'); } //some order form
else { insert_cform('2'); }
?>
正确插入日期,但脚本的其余部分无法正常工作。 我编写了编号,对不起,如果我的问题很愚蠢。
答案 0 :(得分:1)
Wordpress AJAX并没有像这样处理。没有任何东西加载你的WordPress环境,因此the_title()
之类的功能不存在(你可能会在那里遇到致命的错误)。通常,您会创建一个插件set up your AJAX inside
jQuery(document).ready(function($) {
var data = {
'action': 'my_action',
'whatever': 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
$.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
然后你的插件看起来像这样
<?php
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
echo the_title();
die();
}
答案 1 :(得分:0)
正如@vch在评论中所说,如果你有一个简单的php文件,你直接调用那么wordpress框架没有加载,你不能在该文件中使用wordpress函数。
我认为你想要做的是写一个wordpress插件,它暴露了一个ajax动作,然后你可以从你的javascript调用以获得所需的数据。有information in the codex about how to create ajax actions in plugins.
也就是说,如果你打电话给你的ajax动作,你不一定在wordpress循环中,所以我不确定你是否可以使用the_title()
- ajax调用(作为你的.load()
ajax call)是一个新请求,那么服务器如何知道当前帖子是哪个?您可能需要在主页面中获取post id并将其设置为soemwhere数据并将其发送到ajax调用中。