2

Obviously, I know I can't have two single.php files. The one with the comments would have to have a different name...maybe single-comments.php or something.

I am working on a restaurant reviews site and because of lack of space on the page, I have decided to show some excerpts of the latest comments (customer reviews) on the side of the page and then create a link to all the other comments (customer reviews) and the comment form on another page. I have thought of lightboxes but I don't think it is ideal for what I want to achieve.

Any suggestions on how to get this done? Thanks.

EDIT: I created a page template for the comments and named it comments-side.php. Here is the code:

<?php
/*
 * Template Name: Comments Page
 */
 global $data; //get theme options

?>

<div id="comments-template">

    <div class="comments-wrap">

        <div id="comments">

            <?php if ( have_comments() ) : ?>

                <h3 id="comments-number" class="comments-header"><?php comments_number( __( 'No Comments', 'supreme' ), __( 'One Comment', 'supreme' ), __( '% Comments', 'supreme' ) ); ?></h3>

                <?php do_atomic( 'before_comment_list' );// supreme_before_comment_list ?>

                <?php if ( get_option( 'page_comments' ) ) : ?>
                    <div class="comment-navigation comment-pagination">
                        <span class="page-numbers"><?php printf( __( 'Page %1$s of %2$s', 'supreme' ), ( get_query_var( 'cpage' ) ? absint( get_query_var( 'cpage' ) ) : 1 ), get_comment_pages_count() ); ?></span>
                        <?php paginate_comments_links(); ?>
                    </div><!-- .comment-navigation -->
                <?php endif; ?>

                <ol class="comment-list">
                    <?php wp_list_comments( hybrid_list_comments_args() ); ?>
                </ol><!-- .comment-list -->

                <?php do_atomic( 'after_comment_list' ); // supreme_after_comment_list ?>

            <?php endif; ?>

            <?php if ( pings_open() && !comments_open() ) : ?>

                <p class="comments-closed pings-open">
                    <?php printf( __( 'Comments are closed, but <a href="%1$s" title="Trackback URL for this post">trackbacks</a> and pingbacks are open.', 'supreme' ), get_trackback_url() ); ?>
                </p><!-- .comments-closed .pings-open -->

            <?php elseif ( !comments_open() ) : ?>

                <p class="comments-closed">
                    <?php _e( 'Comments are closed.', 'supreme' ); ?>
                </p><!-- .comments-closed -->

            <?php endif; ?>

        </div><!-- #comments -->

        <?php $comment_args = array( 'fields' => apply_filters( 'comment_form_default_fields', array(
                        'author' => '<div class="form_row clearfix">' .
                                    '<input id="author" name="author" type="text" value="' .
                                    esc_attr( $commenter['comment_author'] ) . '" size="30"' . @$aria_req . ' PLACEHOLDER="'.__('Your name','supreme').'"/>' .
                                    ( $req ? ' <span class="required">*</span>' : '' ) .
                                    '</div><!-- #form-section-author .form-section -->',
                        'email'  => '<div class="form_row clearfix">' .
                                    '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . @$aria_req . ' PLACEHOLDER="'.__('Email Address','supreme').'"/>' .
                                    ( $req ? ' <span class="required">*</span>' : '' ) .
                            '</div><!-- #form-section-email .form-section -->',
                        'url'    => '<div class="form_row clearfix">' .
                                    '<input id="url" name="url" type="text" value="' . esc_attr(  $commenter['comment_author_url'] ) . '" size="30"' . @$aria_url . ' PLACEHOLDER="'.__('Website','supreme').'"/>'.'</div>')),
                        'comment_field' => '<div class="form_row clearfix">' .
                                    '<textarea id="comments" name="comment" cols="45" rows="8" aria-required="true" PLACEHOLDER="'.__('Comments','supreme').'"></textarea>' .
                                    ( $req ? ' <span class="required">*</span>' : '' ) .
                                    '</div><!-- #form-section-comment .form-section -->',
                        'comment_notes_after' => '',
                        'title_reply' => __( 'Add a comment', 'supreme' ),
                    );
                    if(get_option('default_comment_status') =='open'){
                        comment_form($comment_args); } // Loads the comment form.  ?>

    </div><!-- .comments-wrap -->

</div><!-- #comments-template -->

I created a page in Wordpress' admin and selected the comments page template for that page. This is what I have in functions.php to handle comments:

function mytheme_comment($comment, $args, $depth) {
        $GLOBALS['comment'] = $comment;
        extract($args, EXTR_SKIP);

        if ( 'div' == $args['style'] ) {
            $tag = 'div';
            $add_below = 'comment';
        } else {
            $tag = 'li';
            $add_below = 'div-comment';
        }
?>
        <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
        <?php if ( 'div' != $args['style'] ) : ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
        <?php endif; ?>
        <div class="comment-author vcard">
        <?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
        <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
        </div>
<?php if ($comment->comment_approved == '0') : ?>
        <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
        <br />
<?php endif; ?>

        <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
            <?php
                /* translators: 1: date, 2: time */
                printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','' );
            ?>
        </div>

        <?php comment_text($comment_ID); ?> 

        <a href="comments-side/#comment-<?php comment_ID() ?>" class="comment-more">read more</a>

        <div class="reply">
        <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
        </div>
        <?php if ( 'div' != $args['style'] ) : ?>
        </div>
        <?php endif; ?>
<?php
        }

And this is how I am calling the comments in the side of single-restaurant.php

<div class="sidereviews clearfix"> 
<h3>Recent Reviews</h3>  
<?php comments_template( '/comments-side.php', true ); // Loads the comments-side.php template. ?>
</div><!--/sidereviews-->

There is another comment file (comments.php) that handles comments on the blog. What should I change or get rid of? What am I doing wrong? Thanks.

2 Answers 2

2

Create a page template to display the comments for the particular "review" aka post.

Then you can link to that page and include the post ID in the url and $_GET the post ide from the url on the new page, then query the post and display its comments.

This should point you in the right direction atleast. The Final Template Kwame Ended With:

<?php
    /*
    Template Name: Handle Review Comments
    */
    get_header(); ?>

    <?php

    echo '<div id="content" class="barsclubs-single">';
    if(isset($_GET['location'])){
          $location = $_GET['location'];
          $postIdVariable = $location;
          $post = get_post( $postIdVariable );    
    echo 'You are viewing reviews for '.'<a href="'.get_post_permalink().'">'.$post->post_title.'</a>';
          $location = $_GET['location'];
          $defaults = array(
             'post_id' => $location,
             'number' => 10,
              );
    $comments = get_comments($defaults);
    wp_list_comments( $args, $comments );
    comment_form( $args, $location );
    }

    echo '</div>';
    include_once( 'footer.php' );
    ?>

Create a page with the template. And send variables to it like so.

www.yoursite.com/yourpagename/?test=postid

$url='www.yoursite.com/yourpagename/?test='.$post->ID;
echo '<a href="'.$url.' title="View Review Comments" target="_BLANK"';

Would send the id of the current post to your script. Must be used in the loop i believe.

Edit:

You can see it in action here:

The script is pulling the comments for the post with the id of 963 , which is the id I sent it in the url.

http://oq.publicvent.org/handle-review-comments/?test=963

Edit 2: Trying to make this simple for you.

Create a file and name it whatever you would like. Put this code in it:

    <?php
    /*
    Template Name: Handle Review Comments
    */
    include_once( 'header.php' );
    if(isset($_GET['test'])){
    echo 'you passed this script the post id: '.$_GET['test'];
    $test = $_GET['test'];
    $defaults = array(
        'post_id' => $test,
        'number' => 10,
             );
    $comments = get_comments($defaults);
    wp_list_comments( $args, $comments ); 
    comment_form( $args, $test );
    }
    include_once( 'footer.php' );
    ?>

Save and upload it your server. Create a new page and name it all-reviews, use the ( Handle Review Comments ) template for the page template, select it from the drop down list on the right.

Now on your post.php or single.php or however you are displaying your review you can use this to display the 5 most recent comments for that review. Inlcuding a link at the bottom to the rest of the reviews.

<div class="sidereviews clearfix"> 
<h3>Recent Reviews</h3>  
<?php 
    $defaults = array(
        'post_id' => $post->ID,
        'number' => 5, //get 5 most recent comments
             );
    $comments = get_comments($defaults);
    wp_list_comments( $args, $comments );
?>
<?php echo '<h3><a href="'.site_url().'/all-reviews/?test='.$post->ID.'">All Reviews</a></h3>'; ?>
</div><!--sidereviews-->

EDIT 3: POST TITLE

Kwame's code:

 $post = get_post( $postIdVariable );    
 echo 'You are viewing reviews for '.$post->post_title;
 $location = $_GET['location'];

$postIdVariable is essentially a empty variable because the $_GET['location']; hasn't run yet.

Change that snippet above to this:

$location = $_GET['location'];
$postIdVariable = $location;    
$post = get_post( $postIdVariable );    
echo 'You are viewing reviews for '.'<a href="'.get_post_permalink().'">'.$post->post_title.'</a>';
14
  • Thanks for your input. I have edited the question with all the code I am working with. I am at a loss of how to implement the solution you provided. I am not a programming whizzkid so I guess I'll need some more hand holding. I hope my edit helps make things a bit clear. Commented Jul 10, 2013 at 15:24
  • I edited my answer and included a working example.
    – Vigs
    Commented Jul 10, 2013 at 17:15
  • It's working now :)....Magic! I'll have to do some layout work with some CSS. Thank you very much. One more question, how can I show the post title? I want to change echo 'you passed this script the post id: '.$_GET['test']; to echo 'You are viewing reviews for '**(post title here)**; Commented Jul 10, 2013 at 19:13
  • Im happy it worked for you :D! Also, i edited my answer to include the post title in the little line there. You can access all kinds of things from the $post object here by repalcing ->post_title with one of the other paramaters listed on this forum post: wordpress.org/support/topic/the-post-object . Feel free to ask me for additional help in th future.
    – Vigs
    Commented Jul 10, 2013 at 20:02
  • Excellent, I'll take a look at the link. Really, Thank you very much. Commented Jul 10, 2013 at 20:03
1

I went away with my family for the weekend. Here is a link to one of the pages using the reviews: http://ghananights.villagepixels.com/bars-clubs/the-republic-bar-and-grill/ (Still needs some styling hehehe)...The permalink is wrongly placed. The link is set between the dates and actually leads nowhere, unlike the "View All reviews at the bottom...How can I show an excerpt of the comment, instead of the full comment and then add a read more link that links to that actual comment? Also, I tried using ->post_title and it returned the title of that page (Location Reviews) instead of the post (The Republic Bar and Grill)...Thanks – Kwame Boame

Attempting to use a custom comment callback to resolve above issue:

Replace:

wp_list_comments( $args, $comments );

With:

$defaults = array(
    'post_id' => $post->ID,
    'number' => 5, //get 5 most recent comments
);

$comments = get_comments($defaults);

$commentsArgs = array(
    'callback'   => 'kwame_comments',
    'reply_text' => null,
);

wp_list_comments( $commentsArgs, $comments );

Place this at the bottom of your functions.php:

function kwame_comments($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    extract($args, EXTR_SKIP);

    if ( 'div' == $args['style'] ) {
        $tag = 'div';
        $add_below = 'comment';
    } else {
        $tag = 'li';
        $add_below = 'div-comment';
    }
?>
    <<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">

    <?php if ( 'div' != $args['style'] ) : ?>
        <div id="div-comment-<?php comment_ID() ?>" class="comment-body">
    <?php endif; ?>

    <div class="comment-author vcard">
        <?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
    </div>

    <?php if ($comment->comment_approved == '0') : ?>
        <em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
        <br />
    <?php endif; ?>

    <div class="comment-meta commentmetadata">
        <a href="<?php echo htmlspecialchars( site_url().'/location-review/?location='.$comment->comment_post_ID.'#comment-'.$comment->comment_ID ); ?>" title="View Review">
        <?php
        /* translators: 1: date, 2: time */
        printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','' );
        ?>
    </div>

    <?php
    $comment_contents = get_comment_text();

    //if comment contents is longer than 50 characters
    if(strlen($comment_contents > 50)){
        echo substr($comment_contents, 0, 50);

        //echo only first 50 characters
        echo $comment_contents;
    } else {
        echo $comment_contents;
    }
?>

    <div class="reply">
        <a href="<?php echo htmlspecialchars( site_url().'/location-review/?location='.$comment->comment_post_ID.'&replytocom='.$comment->comment_ID.'#respond' ); ?>" title="View Review">Reply</a>
    </div>

    <?php if ( 'div' != $args['style'] ) : ?>
        </div>
    <?php endif; ?>

<?php
}

This is working for me on a localhost.

7
  • The comments on the side are not displaying anymore. Don't we have to do add_action(); for it to work? I was also getting errors so I edited the code to add extra parenthesis on the lines with echo #comments and echo #respond. Commented Jul 16, 2013 at 5:23
  • When i get a chance I will run this on my localhost and see how it works, but I am certain a custom comment callback is the right direction to look. Ill update my answers If i get it working.
    – Vigs
    Commented Jul 16, 2013 at 12:52
  • Okay Sir, Thanks. I'll be trying it myself and will let you know what I come up with if I get it working Commented Jul 16, 2013 at 13:12
  • Sounds good, btw compliments on the styleing I thought it looked very nice.
    – Vigs
    Commented Jul 16, 2013 at 13:15
  • Ive got the comment callback to work, But here is some really strange stuff going on with with the links im investigating. I will update the code when i get it 100%.
    – Vigs
    Commented Jul 16, 2013 at 20:39

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.