web-dev-qa-db-fra.com

Impossible d'obtenir_le_contenu (); d'un post dans Wordpress via AJAX

J'essaie d'ajaxifier mon thème Wordpress et j'utilise la méthode décrite ici et j'essaie maintenant d'obtenir get_the_content of post via functions.php. Avec jQuery, lorsque je mets en alerte (données), je reçois l’écho 'title' mais pas le contenu de la publication existante que je souhaite (renvoie 0).

La partie 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;
    });

La partie 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'];

    //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;
}
?>

Qu'est-ce que je fais mal? Merci

2
Gab

J'ai eu une solution ici dans un autre post que j'ai fait à propos du même problème que j'ai eu.

0
Gab

get_the_content() doit être utilisé à l'intérieur de la boucle, sinon vous devrez utiliser setup_postdata(); en fournissant l'objet post.

Essayez ce qui suit:

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;
}

Ceci n'a pas été testé

2
Stephen Harris