无法通过AJAX获取WordPress中帖子的内容

时间:2012-02-20 作者:Gab

我正在尝试调整我的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;
}
?>
我做错了什么?谢谢

2 个回复
最合适的回答,由SO网友:Gab 整理而成

我有办法了here 在另一篇帖子中,我提出了与我相同的问题。

SO网友:Stephen Harris

get_the_content() 必须在循环内使用,否则需要使用setup_postdata(); 提供post对象。

请尝试以下操作:

function ajax_action_stuff() {
    $post_id = $_POST[\'post_id\'];

    //Query the post and set-up data
    $post = get_post($post_id);
    setup_postdata( $post );

    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;
}
这还没有经过测试

结束

相关推荐

WP 3.3.1无法使jQuery Datepicker工作

首先我想告诉你HAVE 阅读帖子:jQuery UI Datepicker not working已经我按照指示做了,但我还是做不到。我将WP 3.3.1与标准主题twentyeleven一起使用。还有另外两个jquery正在运行:“jExpand.js”和“sorttable.js”,就是这样。非常简单。在函数中,我添加了以下内容:add_action( \'wp_print_scripts\', \'wp33r01_date_picker\' ); function wp33r01_