How can I display a random comment from a PAGE on my site on home?
-
I wish to show a random comment from a page titled ‘testimonials’ on the home page of my site.
So far I’ve installed a plugin which let’s me execute PHP within the_content() but I have no idea what PHP I need to accomplish this as I’m still a wordpress newbie.
Can someone help me out here please?
Thanks ??
~gpcola
-
This could involve a fair amount of coding, and I’m not sure your plugin will help.
I think you need to modify the template used to display the Home page. You could get an array of comments from the ‘testimonials’ page by using get_comments(). Then pick one of the comments by using a random subscript. Here is a script that should get you started;
<?php $post_id = 2; // Put the 'testimonials' id here $comments = get_comments("post_id=$post_id&status=approve"); if ($comments) { $ndx = mt_rand(1,sizeof($comments)) - 1; $comment = $comments[$ndx]; <div class='comment_wrapper'> <div class='comment_author'> <?php echo "Comment By:$comment->comment_author"; ?> </div> <div class='comment_content'> <?php echo $comment->comment_content; ?> </div> </div> <?php } ?>
Excellent! Your code snippet was exactly what I needed ?? Creating a new template for the home page wasn’t a problem so all I needed to do was cut and paste your code, bar the addition of a missing ‘?>’.
Thank you for your swift response vtxyzzy!
My template code is now as follows:
<?php /* Template Name: simple */ ?> <?php get_header(); ?> <div class="art-contentLayout-home"> <div class="content"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="post"> <div class="post-body"> <div class="post-inner art-article"> <div class="postContent"> <div> <div><img src="..." width="304" height="141" alt="" /> <?php $post_id = 11; // Put the 'testimonials' id here $comments = get_comments("post_id=$post_id&status=approve"); if ($comments) { $ndx = mt_rand(1,sizeof($comments)) - 1; $comment = $comments[$ndx]; ?> <div class='comment_wrapper'> <div class='comment_author'> <?php echo "Comment By:$comment->comment_author"; ?> </div> <div class='comment_content'> <?php echo $comment->comment_content; ?> </div> </div> <?php } ?> <div class="cleared"></div> </div> <?php remove_filter ('the_content', 'wpautop'); ?> <?php the_content(); ?> </div> </div> </div> </div> </div> <?php endwhile; ?> <?php endif; ?> </div> <div class="cleared"></div> </div> <?php get_footer(); ?>
Glad it helped!
After playing around a little more I was able to use the inline-php plugin I wanted to use originally. By modifying my template code and outputting the comment and author to vars like so:
<?php /* Template Name: simple */ ?> <?php get_header(); ?> <div class="art-contentLayout-home"> <div class="art-content"> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="art-Post"> <div class="art-Post-body"> <div class="art-Post-inner art-article"> <div class="art-PostContent"> <?php $theAuthor=""; $theComment=""; $post_id = 11; // Put the 'testimonials' id here $comments = get_comments("post_id=$post_id&status=approve"); if ($comments) { $ndx = mt_rand(1,sizeof($comments)) - 1; $comment = $comments[$ndx]; $theAuthor=$comment->comment_author; $theComment=$comment->comment_content; } ?> <?php remove_filter ('the_content', 'wpautop'); ?> <?php the_content(); ?> </div> </div> </div> </div> <?php endwhile; ?> <?php endif; ?> </div> <div class="cleared"></div> </div> <?php get_footer(); ?>
I was then able to reference them in the_content() like so:
<div class='comment_wrapper'> <div class='comment_author'> <?php global $theAuthor, $theComment; ?> <?php echo $theAuthor; ?> </div> <div class='comment_content'> <?php echo $theComment; ?> </div> </div> <div class="cleared"></div> </div>
??
This makes it a little easier for me and any other editors to redesign the home page later on.
If you really want to streamline it, you might consider using shortcodes. Then you wouldn’t need to use the exec PHP in your posts, eliminating the security risk that poses (although that might be handled by your plugin).
Aha! Now that looks like it would fly!
Thanks again vtxyzzy – being new to WordPress I don’t really know what functionality I have available to me by default so using a plugin seemed like the obvious choice but looking at how shortcodes work it looks like I can eliminate that extra overhead and use what wordpress provides as standard ??
et voilà!
// display a random comment function randomComment_handler($post_id) { extract( shortcode_atts( array( 'post_id' => '0', ), $atts ) ); $out = ""; $comments = get_comments("post_id=$post_id&status=approve"); if ($comments) { $ndx = mt_rand(1,sizeof($comments)) - 1; $comment = $comments[$ndx]; $out = "<div><div class='comment_wrapper'><div class='comment_author'>".$comment->comment_author."</div><div class='comment_content'>".$comment->comment_content."</div></div><div class='cleared'></div></div>"; } return $out; } add_shortcode('randomComment', 'randomComment_handler');
now I only need to add this to my page/posts to display a random comment:
[randomComment post_id="11"]
vtxyzzy you have been more than helpful! Thank you!
You are welcome!
Thanks for this. I have it working but was wondering, how can I make the author’s name link back to the website they provided in their comment? I’m still trying to figure this out but any help would be appreciated.
Never mind, I figured it out already. If anyone is interested just change:
<div class='comment_author'> <?php echo "Comment By:$comment->comment_author"; ?> </div>
to:
<div class='comment_author'><a href="<?php echo "$comment->comment_author_url"; ?>"><?php echo "Comment By:$comment->comment_author"; ?></a></div>
Hi,
I’m not seeing a lot of information regarding ” random comment “.
On a site I’m using the “recent comments widget”, and would like to have “random comments” from a ” category “, instead. I would like them formated as the same way as the WP widget ( author + post name link )
Is there already a widget doing this, or would I need to use a text widget with a modified code snippet.
Thanks for any cue.
Pierre
You should start a new thread for this question.
The previous answers are all subtly wrong.
This code:
mt_rand(1,sizeof($comments)) - 1;
Will never return the number zero, and never return the first record in your database. The code should be:
mt_rand(0,sizeof($comments)) - 1;
- The topic ‘How can I display a random comment from a PAGE on my site on home?’ is closed to new replies.