0

I am trying to figure out how to echo 4 created fields from a plugin in my theme template for the comment form and customize the comments output as well. The plugin I am using lets me add custom fields to comment_form but I am unable to echo out even one of them in the comments.

The field I am trying to echo in the code below is "$ag_condition". I also tried using the following code in a few variations and couldn't come up with anything that works.

<?php
$args = array(
'orderby' => 'comment_date',
    'order' => 'DESC',
'post_type' => 'property',
);
// return a single meta value with the key 'vote' from a defined comment object
$ag_condition = get_comment_meta( $comment->comment_ID, 'agents_condition', true );
$comments = get_comments($args);
foreach($comments as $comment) :
    echo('<p>' . $comment->comment_author . '</p><p>' . $comment->comment_content . '</p>');
echo ($ag_condition);
endforeach;
?>

I tried seeking support from the plugin forum and this is the only response I got -

Where $meta['data_name'] is the extra field name you set in our plugin.

echo $comment_meta_key = $meta['data_name'];
                $comment_meta_val = get_comment_meta($comment -> comment_ID, $comment_meta_key, true);

My full code --

<?php
$args = array(
'orderby' => 'comment_date',
    'order' => 'DESC',
'post_type' => 'property',

);
// return a single meta value with the key 'vote' from a defined comment object
$ag_condition = get_comment_meta( $comment->comment_ID, 'agents_condition', true );
$comments = get_comments($args);
foreach($comments as $comment) :
    echo('<div id="' . $comment->comment_ID . '">Agent Name ' . $comment->comment_author . 'Phone' . $comment->comment_phone . 'Reply' . $comment->comment_content . '</div>');
echo ($ag_condition);
endforeach;

?><?php comment_form(); ?>

Im not sure how to correct it, any help is appreciated.

UPDATE

I tried the code and tried this other code I was trying to use to echo a custom field from the users profile (phone number) with their comment replies.

In the comment list I just call on the function.

>

 function format_comment() { 
>     echo '<div class="comment">
>         <p>'. ' $comment_author '.'</p>
>         <p>'.'get_comment_meta( $comment->comment_ID, "phone", true )'.'</p> </div>' }

2 Answers 2

0

There is no comment_ID until you are in the foreach loop. Try:

$comments = get_comments($args);

foreach($comments as $comment) :
    echo '<div id="' . $comment->comment_ID . '">Agent Name ' . $comment->comment_author . 'Phone' . $comment->comment_phone . 'Reply' . $comment->comment_content . '</div>';
    echo get_comment_meta( $comment->comment_ID, 'agents_condition', true );
endforeach;
1
  • Hi tried the above code but it still didnt output anything.
    – 730wavy
    Aug 30, 2015 at 4:07
0

You could try:

function add_ag_condition($comment_text) {

    global $comment;

    $ag_condition = get_comment_meta( $comment->comment_ID, 'agents_condition', true );

    return $comment_text . $ag_condition;

}

add_filter('comment_text','add_ag_condition');

The above would add the "ag condition" after the comment text, which appears to be where you originally wanted it. I cannot, of course, test it, since I don't have your comment_meta set-up and can't check all details. EDIT: (I do have to wonder though about how you're using it.)

You can add it at various locations, for instance after 'comment_reply_link' if you want it at the very end of the comment box. (Look through https://github.com/WordPress/WordPress/blob/master/wp-includes/comment-template.php for other filter options.)

1

Your Answer

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

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