我正在尝试调整我的Wordpress主题,我使用了这个方法described here 我现在正在尝试通过函数获取post的内容。php。使用jQuery,当我发出警报(数据)时,我得到的是“标题”回音,但不是我想要的现有帖子的内容(返回0)。
jQuery部分
$(\'.ajaxed,.ajaxed a,.menu-item-home a,.menu-item-object-page a\').live(\'click\', function(event) {
event.preventDefault();
var link = $(this).attr(\'href\');
var toRemove = MySettings.url;
var rewritepath = link.replace(toRemove,\'\');
var handler = function(data) {
$(\'title\').html($(\'title\', data).html());
$(\'#primary\').html($(\'#primary\', data).html());
$(\'#primary\').hide().fadeIn(\'slow\');
$.address.title(/>([^<]*)<\\/title/.exec(data)[1]);
};
$.post(ajax_object.ajaxurl, {
action: \'ajax_action\',
post_id: $(this).find(\'input.post_id\').attr(\'value\')
},function(data) {
alert(data.title);
alert(data.content);
});
$.address.state(MySettings.path).crawlable(true).value(rewritepath);
return false;
});
功能。php部件
<?php
function javascripts() {
if( !is_admin()){
$blogurl = get_bloginfo(\'url\');
$thumbnail_width = get_option(\'thumbnail_size_w\');
$thumbnail_height = get_option(\'thumbnail_size_h\');
$path = parse_url(get_bloginfo(\'siteurl\'), PHP_URL_PATH);
$url = \'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js\';
wp_deregister_script(\'jquery\');
if (get_transient(\'google_jquery\') == true) {
wp_register_script(\'jquery\', $url, array(), null, true);
}
else {
$resp = wp_remote_head($url);
if (!is_wp_error($resp) && 200 == $resp[\'response\'][\'code\']) {
set_transient(\'google_jquery\', true, 60 * 5);
wp_register_script(\'jquery\', $url, array(), null, true);
}
else {
set_transient(\'google_jquery\', false, 60 * 5);
$url = get_bloginfo(\'wpurl\') . \'/wp-includes/js/jquery/jquery.js\';
wp_register_script(\'jquery\', $url, array(), \'1.7\', true);
}
}
wp_enqueue_script(\'plugins.js\', get_bloginfo(\'template_directory\') . "/js/plugins.js" , array(\'jquery\'));
wp_enqueue_script(\'ajax-script\', get_bloginfo(\'template_directory\') . "/js/scripts.js", array(\'jquery\'));
wp_localize_script(\'ajax-script\', \'ajax_object\', array(\'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
wp_localize_script(\'jquery\', \'MySettings\', array(\'width\' => $thumbnail_width,\'height\' => $thumbnail_height,\'url\' => $blogurl,\'path\' => $path));
}
}
add_action(\'wp_enqueue_scripts\', \'javascripts\');
add_action(\'wp_ajax_ajax_action\', \'ajax_action_stuff\'); // ajax for logged in users
add_action(\'wp_ajax_nopriv_ajax_action\', \'ajax_action_stuff\'); // ajax for not logged in users
function ajax_action_stuff() {
$post_id = $_POST[\'post_id\'];
//Query the post and set-up data
$post = get_post($post_id);
setup_postdata();
update_post_meta($post_id, \'post_key\', \'meta_value\');
//Return response as an array. This will give us data.title and data.content browser-side.
$response = array();
$response[\'title\'] = get_the_title();
$response[\'content\'] = get_the_content();
echo json_encode($response);
exit;
}
?>
我做错了什么?谢谢