ajax - 无法获取内容();通过 AJAX 在 Wordpress 中发布的帖子

标签 ajax wordpress jquery

我正在尝试 ajaxify 我的 Wordpress 主题,我使用 ajax-in-WordPress method我现在正在尝试通过functions.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.post_title);
            alert(data.post_content);
        });
        /*$.ajax({
            url: link,
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                handler(XMLHttpRequest.responseText);
            },
            success: function(data, textStatus, XMLHttpRequest) {
                handler(data, function(){
                });
            }
        });*/
        $.address.state(MySettings.path).crawlable(true).value(rewritepath);
        return false;
    });

functions.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'];
    update_post_meta($post_id, 'post_key', 'meta_value'); //not sure why you need this
    $post_data = get_post($post_id);
    echo json_encode($post_data);
}
?>

我做错了什么?谢谢

最佳答案

如果没有看到代码的整个范围,您可能会在 The Loop 的上下文之外调用 get_the_content() 。如果是这样,则该函数无法理解您想要检索哪个帖子的内容。尝试以这种方式组织函数:

function ajax_action_stuff() {
    $post_id = $_POST['post_id'];
    update_post_meta($post_id, 'post_key', 'meta_value'); //not sure why you need this
    $post_data = get_post($post_id);
    $title = $post_data->post_title;
    $content = $post_data->post_content;
    echo $title;
    echo $content;
}

这里我们使用了get_post()返回包含所有发布数据的对象。

您创建的 jQuery 函数...

function(data) {
    alert(data);
});

...本质上应该在 data 对象中包含一个字符串,其中包含您的标题和内容。

不过,如果您愿意,这里有一个关于如何以更有条理的方式返回数据的建议。

“数据”对象(即您在 php 函数 ajax_action_stuff() 中回显的内容)只是一个字符串值。但问题是,数据的结构并没有真正让 jQuery 能够充分理解和充分利用其潜力。如果您更改 php 函数以返回 JSON 对象,那么您可以单独使用 jQuery 中的所有属性。我将向您展示如何...

function ajax_action_stuff() {
    $post_id = $_POST['post_id'];
    update_post_meta($post_id, 'post_key', 'meta_value'); //not sure why you need this
    $post_data = get_post($post_id);
    echo json_encode($post_data);
}

然后在 jQuery 函数中,您可以访问每个属性,如下所示:

$.post(ajax_object.ajaxurl, {
    action: 'ajax_action',
    post_id: $(this).find('input.post_id').attr('value')
},function(data) {
    alert(data.post_title);
    alert(data.post_content);
});

看看 get_post()函数来查看您可用的所有属性。

关于ajax - 无法获取内容();通过 AJAX 在 Wordpress 中发布的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9365385/

相关文章:

php - 带有请求报价插件和 Woocommerce 示例的 Woocommerce 空购物车问题

javascript - 每 10 秒替换一次 <div> 内容不起作用

php - 使用 php/ajax 选择下拉列表后从数据库获取客户信息

javascript - 如何从 WordPress 主题中删除 JavaScript 文件

html - 如何让 WordPress 20 个主题子菜单扩展到它们的内容 (CSS)?

jquery - 如何停止 JavaScript 以等待延迟完成?

php - 更改表单选择时的样式表 onChange ="change()"

php - 我如何将 php 变量放入 mysql 命令中

javascript - Chrome 路径问题

php - Silex + Ajax + 搜索引擎优化