admin管理员组

文章数量:1122846

I want to load comments by ajax.
I can't call comment_template() functions using an ajax request. I don't want create a new query because I'll lost all inherited args (like all settings option in the admin settings).
Here my code:

ajax request in functions.php

add_action( 'wp_ajax_nopriv_ajax_pagination_get_comments', 'luxuryconcept_get_comments_by_ajax' );
add_action( 'wp_ajax_ajax_pagination_get_comments', 'luxuryconcept_get_comments_by_ajax' );

if ( ! function_exists( 'luxuryconcept_get_comments_by_ajax' ) ) {
    function luxuryconcept_get_comments_by_ajax() {
        check_ajax_referer('get_comments_by_ajax', 'security');
        global $withcomments;
        $withcomments = true; 
        comments_template();
        wp_die();
    }
}

JS function in my script

if(ajaxfrontendobject_get_comments.use_ajax){

    $(document).on( 'click', '.navigationments-pagination .nav-links a', function( event ) {
        event.preventDefault();

        page = get_page_from_url( $(this).attr('href') );

        $.ajax({
            url: ajaxfrontendobject_get_comments.ajaxurl,
            type: 'post',
            data: {
                action: 'ajax_pagination_get_comments',
                security: ajaxfrontendobject_get_comments.security,                   
                page: page
            },
            beforeSend: function() {
                //$(document).scrollTop(0);
                $('#comments').remove();
                $('#comments').find( 'nav' ).remove();               
                $('#content').append( '<div class="posts_loader" id="loader"><div class="loader_icon"></div></div>' );
            },
            success: function( html ) {
                $('#content #loader').remove();
                $('#content').append( html );
                alert("success");
            },
            error: function(){
               alert("error");
            }
        })
    })

}

In the documentation I read that I have to set global $withcomment to true but it doesn't work yet.

Thanks

I want to load comments by ajax.
I can't call comment_template() functions using an ajax request. I don't want create a new query because I'll lost all inherited args (like all settings option in the admin settings).
Here my code:

ajax request in functions.php

add_action( 'wp_ajax_nopriv_ajax_pagination_get_comments', 'luxuryconcept_get_comments_by_ajax' );
add_action( 'wp_ajax_ajax_pagination_get_comments', 'luxuryconcept_get_comments_by_ajax' );

if ( ! function_exists( 'luxuryconcept_get_comments_by_ajax' ) ) {
    function luxuryconcept_get_comments_by_ajax() {
        check_ajax_referer('get_comments_by_ajax', 'security');
        global $withcomments;
        $withcomments = true; 
        comments_template();
        wp_die();
    }
}

JS function in my script

if(ajaxfrontendobject_get_comments.use_ajax){

    $(document).on( 'click', '.navigation.comments-pagination .nav-links a', function( event ) {
        event.preventDefault();

        page = get_page_from_url( $(this).attr('href') );

        $.ajax({
            url: ajaxfrontendobject_get_comments.ajaxurl,
            type: 'post',
            data: {
                action: 'ajax_pagination_get_comments',
                security: ajaxfrontendobject_get_comments.security,                   
                page: page
            },
            beforeSend: function() {
                //$(document).scrollTop(0);
                $('#comments').remove();
                $('#comments').find( 'nav' ).remove();               
                $('#content').append( '<div class="posts_loader" id="loader"><div class="loader_icon"></div></div>' );
            },
            success: function( html ) {
                $('#content #loader').remove();
                $('#content').append( html );
                alert("success");
            },
            error: function(){
               alert("error");
            }
        })
    })

}

In the documentation I read that I have to set global $withcomment to true but it doesn't work yet.

Thanks

Share Improve this question edited Apr 10, 2024 at 12:48 Stefano asked Apr 10, 2024 at 10:47 StefanoStefano 636 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I partially resolved adding this code:

$args = json_decode( stripslashes( $_POST['query_vars'] ), true );     
$args['paged'] = $_POST['page'];    

//Set Query Post args
query_posts( $args );

//Set Glboal Post Data
global $post;
$post = get_post( $_POST['post_id'] );
setup_postdata( $post );

//Set global $withcomment = true to execute comments_template() - using comments-ajax.php Template (Root Folder Theme)
global $withcomments;
$withcomments = true; 
comments_template('/comments-ajax.php');

and this in my js:

post_id: ajaxfrontendobject_get_comments.post_id,

Now it works but if I click on the page number (using ajax request) on which the site initially loaded (without using ajax) no comment is loaded and I don't understand why.

本文标签: functionscommentstemplate() doesn39t work with ajax call