get content from page through AJAX wordpress

I’m using an wordpress theme and I want to load content from a page inside a div from another page.

Here is the code from the page which I want to load inside the div :

Read More

page-usercomments.php (this is a custom page template where I retrieve comments of the current user)

<?php

    get_header();

?>

    <div id="primary" class="row">

        <div id="content" class="span9" role="main">

    <!-- GET CURRENT USER -->

    <?php

    global $current_user;
    get_currentuserinfo();

    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    ?>


   <!-- GET COMMENTS OF USER -->
    <?php 
    $args = array(
    'user_id' => $current_user->ID, // use user_id
    'post_type' => 'debate'
     );
    $comments = get_comments($args);

    ?>
     <ol class="commentlist">
            <?php
              wp_list_comments(
  array(
    'per_page' => 10, //Allow comment pagination
    'reverse_top_level' => false //Show the latest comments at the top of the list
  ), 
  $comments
);
            ?>
        </ol><!-- .commentlist -->
        </div><!-- #content .site-content -->

    </div><!-- #primary .content-area -->

<?php get_footer(); // This fxn gets the footer.php file and renders it ?>

I want to get the output of this page inside a div from another page and I’m thinking to use AJAX and this is the code:

UPDATE:

     $.ajax({
     type: "GET",
     url:"http://www.mywebsite.co/cpt/user-comments",
     cache: false,
     dataType: 'html',
     success: function(data){
               $("#div").append(data);        //   <--- look here
    },
     error: function(){ },
     complete: function(){ }
    });

    });

LATER UPDATE: I’VE RESOLVED THIS BY USING THE WORDPRESS *AJAX API*

Is this the correct way to do it ?

And also, I don’t know how should I add the url because the permalink created for the page which I want to retrieve is “mywebsite.com/user-comments”
Thank you !

Related posts

Leave a Reply

1 comment

  1. Here is my ajax function…

    You send your ajaxcall, and when it return with success code, get the data, and put it into your div.

    $.ajax({
        type: "GET",
        url: yourUrl,
        cache: false,
        dataType: 'html',
        success: function(data){
                   $("#div").append(data);        //   <--- look here
        },
        error: function(){ },
        complete: function(){ }
    });