• Resolved Bernie

    (@fruitshakes)


    I created a page called “My Ratings” where users can see the ratings they made.

    I removed the slashes in front of add_filter('comment_text', 'comment_author_ratings_filter'); and then used wp_list_comments and filtered it to show only the logged in user’s comments and hopefully ratings too, like so:

    <?php
    		//Gather comments for a specific page/post
    		$comments = get_comments(array(
    			'user_id' => get_current_user_id()
    		));
    
    		echo '<ul>';
    
    		wp_list_comments( array( 'callback' => 'display_user_ratings', 'style' => 'ul' ), $comments );
    
    		echo '</ul>';
    
    ?>

    And here’s the display_user_ratings callback (mainly based from twentytwelve_comment):

    if ( ! function_exists( 'display_user_ratings' ) ) :
    /**
     * Template for displaying user_ratings
     *
     */
    function display_user_ratings( $comment, $args, $depth ) {
    	$GLOBALS['comment'] = $comment;
    	switch ( $comment->comment_type ) :
    		case 'pingback' :
    		case 'trackback' :
    		// Display trackbacks differently than normal comments.
    	?>
    	<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
    		<p><?php _e( 'Pingback:', 'twentytwelve' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( '(Edit)', 'twentytwelve' ), '<span class="edit-link">', '</span>' ); ?></p>
    	<?php
    			break;
    		default :
    		// Proceed with normal comments.
    		global $post;
    	?>
    	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
    		<article id="comment-<?php comment_ID(); ?>" class="comment">
    			<header class="comment-meta comment-author vcard">
    				<?php
    
               			echo get_the_title($comment->comment_post_ID);
    
    					echo get_avatar( $comment, 44 );
    					printf( '<cite><b class="fn">%1$s</b> %2$s</cite>',
    						get_comment_author_link(),
    						// If current post author is also comment author, make it known visually.
    						( $comment->user_id === $post->post_author ) ? '<span>' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
    					);
    					printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
    						esc_url( get_comment_link( $comment->comment_ID ) ),
    						get_comment_time( 'c' ),
    						/* translators: 1: date, 2: time */
    						sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
    					);
    				?>
    			</header><!-- .comment-meta -->
    
    			<?php if ( '0' == $comment->comment_approved ) : ?>
    				<p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'twentytwelve' ); ?></p>
    			<?php endif; ?>
    
    			<section class="comment-content comment">
    				<?php comment_text(); ?>
    
    				<?php edit_comment_link( __( 'Edit', 'twentytwelve' ), '<p class="edit-link">', '</p>' ); ?>
    			</section><!-- .comment-content -->
    
    			<div class="reply">
    				<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply', 'twentytwelve' ), 'after' => ' <span>&darr;</span>', 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
    			</div><!-- .reply -->
    		</article><!-- #comment-## -->
    	<?php
    		break;
    	endswitch; // end comment_type check
    }
    endif;

    The callback display_user_ratings is successful in listing comments made by the current user showing the post titles, dates, comment texts etc..except for the ratings. All ratings showed that the user did not rate the posts, though the user rated them all.

    I think this is primarily because the rating is getting the ratings for the page “My Ratings” instead of showing the ratings for each of the comment.

    Is there a way to show the ratings for each comment?

    https://wordpress.org/plugins/wp-postratings/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Show user ratings on a page’ is closed to new replies.