0

I've add the field country in the comment form. Working nice and I can see the value in the table commentmeta.

enter image description here

Now I try to display this value in my comment list.

In my single.php I call the list of comments:

<?php wp_list_comments('type=comment&callback=format_comment'); ?>

In my functions.php I format the comment:

function format_comment() {  ?>
    <div class="comment">
        <p><?php comment_author(); ?></p>
        <p><?php get_comment_meta( $comment->comment_ID, 'country', true ); ?></p>
        <p><?php comment_date('Y/m/d - g:i A'); ?></p>
        <p><?php comment_text(); ?></p>
    </div>
<?php } ?>

So... to get the value for the meta country I use

<?php get_comment_meta( $comment->comment_ID, 'country', true ); ?>

Without success…

3
  • 1
    By "without success," I'm guessing that you mean that your call to get_comment_meta() appears to be returning nothing/NULL/an empty string?
    – bosco
    May 2, 2013 at 6:21
  • Yes that it, sorry for the miss.
    – jjj
    May 2, 2013 at 6:22
  • 1
    No worries - thanks for the exceptional formatting in your post ;)
    – bosco
    May 2, 2013 at 6:29

1 Answer 1

2

Looking at the Codex entry for get_comment_meta(), it appears that when the $single argument is set to TRUE (as you have done), the function returns a string. Try throwing an echo() into the works:

<?php echo get_comment_meta( $comment->comment_ID, 'country', true ); ?>
4
  • helpfull, thank to show me the way. Now it work with <?php echo get_comment_meta( get_comment_ID(), 'country', true ); ?>
    – jjj
    May 2, 2013 at 6:25
  • Glad to hear it! Rock on, jeromeM ;)
    – bosco
    May 2, 2013 at 6:28
  • what if you want to check if is exist first? i.e. not empty...?
    – klewis
    Jan 13, 2021 at 9:44
  • 1
    @klewis it's normally not necessary - the get_comment_meta() call will return false or null in that case, and so the echo won't print out anything. If you do want to check first though, I'd recommend just storing the output in a temporary variable. $country = get_comment_meta( get_comment_ID(), 'country', true ); if( isset( $country ) ) echo $country;
    – bosco
    Jan 13, 2021 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.