admin管理员组

文章数量:1419230

Why this load whole page HTML, not selected post content?

FUNCTIONS.PHP

function my_action_callback() {
    wp_localize_script('main_js', 'ajax_custom', array(
       'ajaxurl' => admin_url('admin-ajax.php')
    ));
}
add_action( 'wp_enqueue_scripts', 'my_action_callback' );

function my_php_function_name() {
    $the_post_id = $_POST['post_id'];
    $args = array(
        'post_type' => 'offer',
        'post_status' => 'publish',
        'p' => $the_post_id
    );
    global $post;
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) { $the_query->the_post(); 
            the_content();
        } wp_reset_postdata(); 
    }
}

MAIN.JS

$(".offer .item").click(function () {
    var post_id = $(this).attr("data-id")
    $("#post-container").html("content loading");
    $.ajax({
        url: ajax_custom.ajax_url,
        type: 'post',
        data: {
            action: 'my_php_function_name',
            post_id: post_id
        },
        success: function (data) {
            console.log(data);
            jQuery('#post-container').html(data);
        }
    });
    return false;
});

Why this load whole page HTML, not selected post content?

FUNCTIONS.PHP

function my_action_callback() {
    wp_localize_script('main_js', 'ajax_custom', array(
       'ajaxurl' => admin_url('admin-ajax.php')
    ));
}
add_action( 'wp_enqueue_scripts', 'my_action_callback' );

function my_php_function_name() {
    $the_post_id = $_POST['post_id'];
    $args = array(
        'post_type' => 'offer',
        'post_status' => 'publish',
        'p' => $the_post_id
    );
    global $post;
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) { $the_query->the_post(); 
            the_content();
        } wp_reset_postdata(); 
    }
}

MAIN.JS

$(".offer .item").click(function () {
    var post_id = $(this).attr("data-id")
    $("#post-container").html("content loading");
    $.ajax({
        url: ajax_custom.ajax_url,
        type: 'post',
        data: {
            action: 'my_php_function_name',
            post_id: post_id
        },
        success: function (data) {
            console.log(data);
            jQuery('#post-container').html(data);
        }
    });
    return false;
});
Share Improve this question asked Jul 23, 2019 at 12:30 Marcin UrbańczykMarcin Urbańczyk 134 bronze badges 2
  • Can you show us where the ajax function my_php_function_name() is registered and also what ajax_custom.ajax_url is set to? – Douglas.Sesar Commented Jul 23, 2019 at 19:33
  • Code above is everything /i have, sorry, ajax in wp is completly new for me – Marcin Urbańczyk Commented Jul 24, 2019 at 15:25
Add a comment  | 

1 Answer 1

Reset to default 0

If all you want is the content of one post:

$post   = get_post( $id );

$output =  apply_filters( 'the_content', $post->post_content );

ob_clean();

echo $output;

die();

本文标签: Ajax load content from post I clicked