首页 文章

无法get_the_content();通过AJAX在Wordpress中发布的帖子

提问于
浏览
2

我正在尝试ajaxify我的Wordpress主题,我使用ajax-in-WordPress method和我'm now trying get_the_content of post via functions.php. Using jQuery, when I do alert(data) I get the ' Headers '回声但不是我想要的现有帖子的内容(返回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);
}
?>

我究竟做错了什么?谢谢

2 回答

  • 2

    您没有告诉 get_the_content() 要检索哪些帖子的内容 . 在内部,此函数检查全局 $post 对象并过滤该对象的内容 .

    所以将你的ajax函数改为这样的:

    function ajax_action_stuff() {
        global $post;
    
        $post_id = $_POST[ 'post_id' ];
        update_post_meta( $post_id, 'post_key', 'meta_value' );
    
        $post = get_post( $post_id );
    
        $title = 'title';
        $content = get_the_content();
    
        echo $title;
        echo $content;
    }
    

    这将使用您传入的ID来查询特定帖子的数据库并填充全局 $post 对象 . 现在, get_the_content() 甚至 get_the_title() 应该正常运行 .

  • 2

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

    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 对象中包含一个包含 Headers 和内容的字符串 .

    这里有一个建议,如果你愿意,你可以如何以更有条理的方式返回数据 .

    'data'对象(这是你在php函数 ajax_action_stuff() 中回显的)只是一个字符串值 . 但问题是数据不是't really structured in a way for jQuery to fully understand and use to its full potential. If you change your php function to return a JSON object though, then you can use all your properties in jQuery individually. I' ll告诉你如何......

    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()函数以查看您可以使用的所有属性 .

相关问题