Pregunta

I use the following function to get the latest comment for each one of my posts and display it on archive and index pages under the post content:

function kv_latest_comment($post){
         $args = array(
            'status' => 'approve',
            'number' => '1',
            'post_id' => $post->ID, // use post_id, not post_ID
              );
            $comments = get_comments( $args );
        if($comments){
        foreach($comments as $comment) :
        if($comment->comment_type != 'pingback' and $comment->comment_type != 'trackback') : ?>
        <div class="comment">
           <div class="comment-avatar"><?php echo get_avatar($comment,$size='64'); ?></div><!-- comment-avatar -->
           <div class="comment-right">
           <div class="comment-bubble comment-bubble-left">
           <div class="comment-header">
              <a href="<?php echo site_url().'/author/'.get_the_author_meta( 'user_login'); ?>" title=""><?php printf(__('%s'), get_the_author_meta( 'user_login')) ?></a> on <?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?><?php edit_comment_link(__('(Edit)'),'  ','') ?>
           </div><!-- comment-header -->
           <div class="comment-content">
              <?php echo($comment->comment_content); ?>
           </div><!-- comment-content -->
           </div><!-- comment-bubble -->
           </div><!-- comment-right -->
           <div class="clear"></div>
        </div><!-- comment -->
<?php
        endif;
        endforeach; }
}

I call the function like so in my post template:

<?php kv_latest_comment($post); ?>

When calling the function from functions.php comment_date() and comment_time() return nothing.

But if I paste the code from the function into the template it works as expected...

Why don't the functions return any values when called from the functions.php?

¿Fue útil?

Solución

get_comment_date requires a comment id as the second argument. If you don't provide this, it looks to the global $comment variable, which is set when using a WP_Query->the_comment(). Maybe consider changing your loop to be a WP_Query, or just do get_comment_date( '', $comment->ID )

Otros consejos

Beacause you are not passing the post object, on a template you have the $post, but if you run the function from the function.php file you have no $post. You can add global $post; to the function like this:

function kv_latest_comment($post) {
    global $post;
    // code
}

lol. I have similar problem. Finally figure this out:

$aa = get_comments( array( 'post_id' => 27 ) );

foreach ($aa as $comment):

echo comment_time( 'H:i:s' );

endforeach;

The most important key part is that $comment is a global variable that I am override into. In the beginning I thought it was just separating out array of comments into individual comment so I can name that variable as $bb or whatever. THAT was wrong, only when it is $comment, all those functions that apply to CURRENT comment works. :)

Licenciado bajo: CC-BY-SA con atribución
scroll top