0

I'm getting 5 recent comments into my sidebar using the following code:

<?php $recent_comments = get_comments('status=approve&number=5'); ?>
<?php foreach ($recent_comments as $comment) { ?>
    <li>
        <a href="#"><?php echo get_avatar( $comment, '60' ); ?> </a>
        <div>
            <a href="<?php echo get_permalink($comment->ID); ?>" title="on <?php echo $comment->post_title; ?>"><?php echo strip_tags($comment->comment_author); ?>   </a><br />
            <span><?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>...</span>
        </div>
    </li>
<?php }  ?>

But the problem that I am facing is: When I click on the comment link it's redirecting me to the current page instead of the page that is commented. On other hand, when I click on the comment link (that is in sidebar) in homepage, it's redirecting me to the latest post page.

I have used wp_reset_query() but the problem persists.
Also usingt get_comment($comment->ID); instead of get_permalink is not working.

1 Answer 1

1

Try this code , you have not used the correct output variables.

<?php $recent_comments = get_comments('status=approve&number=5'); ?>
          <?php foreach ($recent_comments as $comment) { ?>
            <li>
                <a href="#"><?php echo get_avatar( $comment, '60' ); ?> </a>
                <div>
                    <a href="<?php echo get_permalink($comment->comment_post_ID); ?>" title="on <?php echo get_the_title($comment->comment_post_ID); ?>"><?php echo strip_tags($comment->comment_author); ?>   </a><br />
                    <span><?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>...</span>
                </div>

            </li>
        <?php }  ?>
0

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.