• Resolved rebekahford

    (@rebekahford)


    I’m currently calling the following to render random thumbnail posts on my index page and category pages

    <?php query_posts(array('orderby' => 'rand', 'category_name' => 'All', 'showposts' => 4)); if (have_posts()) : while (have_posts()) : the_post(); ?>
      <?php
    	 if ( has_post_thumbnail() ) {
    	the_post_thumbnail();
    	  } else {
    	// the current post lacks a thumbnail
    	echo 'no thumbnail dufus!';
    	 }
    ?>
    <?php endwhile; else: ?>
    ...
    <?php endif; ?>

    I also want to add another loop in these templates to generate 3 or 4 random comment excerpts. I’ve tried all sorts of things but can’t seem to work out how to generate only a few random comment excerpts and by category as I am not familiar with all of the wordpress gubbins around commenting.

    I was thinking that the simplest would be to add another post loop underneath the main post loop on the page so that it would pick comments from random posts first, by a category name called ‘All’, then show one comment per each of those 4 random posts. I don’t want them to be the same random posts that are in the first post loop.

    <?php query_posts(array('orderby' => 'rand', 'category_name' => 'All', 'showposts' => 3)); if (have_posts()) : while (have_posts()) : the_post(); ?>
       //a 'foreach' command goes here for a comment array?????
       <?php comment_excerpt(); ?>
       //end a foreach?????
    <?php endwhile; else: ?>
    ...
    <?php endif; ?>

    Will this work?

    If so, can someone tell me what i need to add around <?php comment_excerpt(); ?> to make sure it only shows one comment per post and what i need to envelope around <?php comment_excerpt(); ?> to get it to show in the first place?

    I will send curlywurlys or any other quaint english chocolate to anyone who can help me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter rebekahford

    (@rebekahford)

    thanks i’ll try that… didn’t see those examples before

    Thread Starter rebekahford

    (@rebekahford)

    That hasn’t quite answered my problem. I need to know how to call the comment excerpt from outside single.php in the first place but that piece of code is very helpful thank you

    Thread Starter rebekahford

    (@rebekahford)

    I have got this to work but have a little problem. I have the first post loop going ok with 4 random thumbnail posts. Then after this I have another loop with 3 posts but outputting an array of approved comments. However when it calls a post with no comment it’s displaying an empty box. I need to add a php conditional to say something like ‘if there is a comment display a comment,else display nothing but i wonder if there’s a more efficient way of structuring the following code to do this than wrapping it around the <?php $comment_array =… part of the following.

    Any suggestions welcome and chocolate will be forthcoming ??

    <!-- start comment array -->
            <?php query_posts(array('orderby' => 'rand', 'category_name' => 'All', 'showposts' => 3, 'offset'=>4)); if (have_posts()) : while (have_posts()) : the_post(); ?>
                <li id="post-<?php the_ID() ?>" class="text <?php veryplaintxt_post_class() ?>">
                      <a href="<?php the_permalink() ?>?KeepThis=true&height=450&width=820&modal=true" title="<?php printf(__('%s', 'veryplaintxt'), wp_specialchars(get_the_title(), 1)) ?>" class="thickbox">
                                <?php
    								$comment_array = array_reverse(get_approved_comments($wp_query->post->ID));
    								$count = 1;
    									if ($comment_array) {
    										foreach($comment_array as $comment) {
    											if ($count++ <= 1) {
    									?>
    										<?php comment_excerpt(); ?>
    							<?php } } } ?>
                       </a>
                 </li>
    		<?php endwhile; endif; ?>
                <!-- end comment array -->
    Thread Starter rebekahford

    (@rebekahford)

    fixed it! it’s me i’m an idiot and have rearranged my code to this…

    ` <!– start comment array –>
    <?php query_posts(array(‘orderby’ => ‘rand’, ‘category_name’ => ‘All’, ‘showposts’ => 3, ‘offset’=>4)); if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php
    $comment_array = array_reverse(get_approved_comments($wp_query->post->ID));
    $count = 1;
    if ($comment_array) {
    foreach($comment_array as $comment) {
    if ($count++ <= 1) {
    ?>
    <li id=”post-<?php the_ID() ?>” class=”text <?php veryplaintxt_post_class() ?>”>
    <a href=”<?php the_permalink() ?>?KeepThis=true&height=450&width=820&modal=true” title=”<?php printf(__(‘%s’, ‘veryplaintxt’), wp_specialchars(get_the_title(), 1)) ?>” class=”thickbox”>
    <?php comment_excerpt(); ?>
    </a>
    </li>
    <?php } } } ?>
    <?php endwhile; endif; ?>
    <!– end comment array –>`

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Adding random comment excerpts to a random posts loop’ is closed to new replies.