WordPress.org

Ready to get started?Download WordPress

Forums

Comments Template and AJAX (6 posts)

  1. benpierce23
    Member
    Posted 3 years ago #

    Hi,

    I've been having a problem getting comments_template to work when calling it in a function in theme functions using AJAX. It works fine when it is called for the first page load, but not when called during AJAX. I'm thinking that there's some include that's missing, but I don't know enough here to understand what.

    Here's the essence of the function code that's in my functions.php file for my Theme. (the entire thing much longer)

    function displayLargePost ($postid) {
    
    	// get the submitted postid parameter if set.
    	if (!$postid) {
    		$postid = $_REQUEST['postID'];
    	}
    
    	$myposts = new WP_Query();
    	$myposts->query( 'p='.$postid );
    	while( $myposts->have_posts() ) : $myposts->the_post();
    
      // some formatting stuff is done then output post content
    
      the_content();
    
      // some more formatting then output the comments template (doesn't work with AJAX)
    
      comments_template();
    
    }

    Again, the function executes when the AJAX call is run, everything works except for the comments_template outputs '0'.

    Thanks for any help!

  2. benpierce23
    Member
    Posted 3 years ago #

    Ok, I figured out a few things.

    1) wp-load.php is key to setting up wordpress and is in fact being loaded in the admin-ajax.php file using require_once();

    2) Somehow, within my function in functions.php, the comments_template() is not even being recognized as a function. But if I force the comments.php file using include('comments.php'); then it actually processes that php code.

    3) Even still when it processes that php code, it does not recognize the global $post, $user_ID and $user_identity variables so I have to re-globalize them locally to get stuff to work using...

    <?php global $post, $user_ID, $user_identity; ?>

    I still have no idea why this is all happening. Maybe something to do with the load not getting the comments-template.php file included or something.

    I'm surprised nobody has ever had this issue, but I can't find it anywhere.

  3. Michael Fields
    Themer
    Posted 3 years ago #

    This is probably a good place to start:

    function testtesttest() {
    	$post_id = ( isset( $_GET['post_id'] ) ) ? (int) $_GET['post_id'] : null;
    	query_posts( array(	'p' => $post_id ) );
    	if ( have_posts() ) {
    		while( have_posts() ) {
    			the_post();
    			the_title();
    			the_content();
    			comments_template( '', true );
    		}
    	}
    }
    add_action( 'wp_ajax_testtesttest', 'testtesttest' );
  4. Michael Fields
    Themer
    Posted 3 years ago #

    This one's a bit better:

    /**
     * Show a single post via admin-ajax.php
     *
     * Use a url like this to call this function:
     * /wp-admin/admin-ajax.php?action=mfields_show_post&p=514
     *
     * @since     2010-12-04
     */
    function mfields_ajax_show_post() {
    	$id = ( isset( $_GET['p'] ) ) ? (int) $_GET['p'] : false;
    	query_posts( array( 'p' => $id ) );
    	if ( have_posts() ) {
    		while( have_posts() ) {
    			the_post();
    			the_title();
    			the_content();
    			comments_template( '', true );
    		}
    	}
    	else {
    		print '<p>No posts</p>';
    	}
    	exit;
    }
    add_action( 'wp_ajax_mfields_show_post', 'mfields_ajax_show_post' );
    add_action( 'wp_ajax_nopriv_mfields_show_post', 'mfields_ajax_show_post' );
  5. art386
    Member
    Posted 3 years ago #

    Respect! Thank you))

  6. KouiK
    Member
    Posted 2 years ago #

    Respect +1 !!

    Exactly what i was searching for. Thanks !

Topic Closed

This topic has been closed to new replies.

About this Topic