A
A
apptimeru2015-08-12 22:33:25
JavaScript
apptimeru, 2015-08-12 22:33:25

Ajax Loading WP comments?

Good evening gentlemen. I want to load a block with comments on Wordpress when scrolling the page to the end, I ran into a problem, I can’t find a solution on the net. The problem is the following, when loading the WP comments_template() function via Ajax, the function does not work because it does not get global variables such as $post. The comments_template function itself does not accept such parameters, as it is usually used in a loop, and receives the necessary data from global variables.

How to solve a similar problem? Is it possible to use this function, or do I need to do everything manually? pass the post ID, pull out all the comments on it and the commenting form itself.

Thank you in advance for your help)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yarchick, 2015-10-16
@YarchicK

function comm(){	 
  $p_id = ( $_POST['spd'] );	
  $args = array(
    'posts_per_page' => -1,
    'p' => $p_id
  );
  $yy_query = new WP_Query($args);	
  while ( $yy_query->have_posts() ) {
    $yy_query->the_post();
global $withcomments;
    $withcomments = true;
     comments_template();		
  }	
 wp_die();
}
add_action('wp_ajax_mycomm', 'comm');
add_action('wp_ajax_nopriv_mycomm', 'comm');

---
$('.seecomments').click(function(){	
 	$.ajax({ 		
 		url: ajaxurl, 
 		type: 'POST',		
 		beforeSend: (function(){				
          $('.comment_template').html('Загрузка..');
      }),
 		data: ({
 			action: 'mycomm',	
 			spd: send_post_id 			
 		}),
 		success: function(data){ 		 
 			$('.comment_template').html(data);
 		}
 	})
 });

---
In single.php
//code...
<div class="seecomments">показать комменты</div>
<script>
  var send_post_id = <?php the_ID(); ?>;
</script>
//code..

I
Igor Vorotnev, 2015-08-12
@HeadOnFire

Try

global $withcomments;
$withcomments = true;
comments_template();

M
Maxim, 2015-08-13
@Qumbeez

Use Disqus.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question