Showing custom comments saved values to visitors

Hi everyone
It’s been a long time since I wrote something
Due to requests for showing the custom fields in the template I searched a little bit and finally found the easiest solution
So what should you do?

First of all
Go to you template directory (in default template it’s “wp-content/themes/twentyten”)
Second
Open comments.php and search for “wp_list_comments”(Don’t search with quotes :P)
Third
Do you see the “something” in wp_list_comments( array( ‘callback’ => ‘something’ ) );?
That’s the function that will be overwrite the default template for comments
If it’s not there add it and instead of something write the name you like
Fourth
Now open functions.php
If you haven’t changed anything in comments.php so search for “something”(the name of the function)
Now you you should add this code to this function wherever you want(put it in different place to see where it will appear-at the beginning it will be before author and at the end after the reply link-)

The function


$CComment_fields = array_filter(explode(",", get_option('CComment_fields')));
foreach ($CComment_fields as $CComment_field){
$CComment_field_arr = explode(";CuCo;", get_option($CComment_field));
$CComment_field_desc = $CComment_field_arr[0];
$CComment_field_active = $CComment_field_arr[1];
$Comment_field_value = get_comment_meta(get_comment_ID(), "CuCo_".$CComment_field);
if ($CComment_field_active == 1)
echo $CComment_field_desc.' : '. (($Comment_field_value[0] != '') ? $Comment_field_value[0] : '-');
}

Do you see this line “echo $CComment_field_desc.’ : ‘. (($Comment_field_value[0] != ”) ? $Comment_field_value[0] : ‘-‘);”?
It’s the part that prints each of custom field and it’s value
Just play with it as you please(add class, id or whatever you want)
For example
echo ‘
<div class=”cc-field”>’ . $CComment_field_desc .</div>
. ‘ : ‘. (($Comment_field_value[0] != ”) ? $Comment_field_value[0] : ‘-‘);
I hope this helps
Thanks for all of the comments about this plugin 🙂

Advertisements

, , ,

  1. #1 by Sasha on 01/12/2011 - 11:43 PM

    I’ve been researching this issue for a long-long time now and when I almost got to the solution (above) I can’t implement it.

    I didn’t have a function name in wp_list_comments( array( ‘callback’ => ‘something’ ) );?

    So I renamed ‘something’ to ‘comments_titles’.

    But now I don’t know how to paste the function code in the functions.php

    Shouldn’t it look like something similar to this:

    add_filter(‘preprocess_comment’,’mm_unit_comment_type’);

    function mm_unit_comment_type($commentdata){
    global $post;
    if ($post->post_type == ‘units’ {
    $commentdata[‘comment_type’] = ‘unit-report’;
    return $commentdata;
    }
    else{
    return $commentdata;
    }
    }

    I know I need to call a function or pass some variables ( I know it just in theory), but I don’t know how should my function look in functions.php?

    I would appreciate your help!

    • #2 by Im a programmer on 02/12/2011 - 7:39 AM

      Hi
      First of all can you tell which version of wp you are using?
      And if you don’t mind send functions.php and comments.php files in you theme folder to moein.ak [at] gmail [dot] com so I could help you better 😉

  2. #3 by Josh Maxwell on 06/07/2012 - 5:26 PM

    I’ve come up with a simple way to display the custom comment fields in a “usable” way.

    First, follow the steps from the above post, but when you get to the `functions.php` file and the comments template, you can add this pastebin code to whatever area you want in the template.

    My code is from a custom guestbook using the built-in WP comment system from a bed and breakfast site. It displays where the user was from (city, state) and the dates they stayed at the B&B.

    Change the variables to match your own fields.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: