• Hi there,
    i used this function to show the most-commented posts in my blog.

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 3");
    							foreach ($result as $post) {
    							setup_postdata($post);
    							$postid = $post->ID;
    							$title = $post->post_title;
    							$commentcount = $post->comment_count;
    							if ($commentcount != 0) { ?>
    
    					<div class="mostCommentedBox">
    							<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php
    						if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    						  the_post_thumbnail();
    						} else { ?>
    
    						<img src="<?php bloginfo('template_directory'); ?>/images/noimage.jpg">
    
    						<?}?> </a>
    						<strong><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></strong>
    						<div class="mostCommentedBoxCommetsCount" title="<?php echo $commentcount ?> comments"><?php echo $commentcount ?></div>
    							<p><?php the_excerpt(); ?></p>
    
    					</div>
    
    					<?php } } ?>

    but for some reasone i don’t see the excerpt at all….
    i’m sure it’s something stupid i’m missing… help, anyone? 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • In the database query you must select the excerpt.

    Replace:

    $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 3");

    With:

    $result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_excerpt FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 3");

    And try!

    Thread Starter udimagnet

    (@udimagnet)

    Thanks for the reply but…Nope, still doesnt work… 🙁

    There are many errors, for example:

    1) On line 17, this <?}?> must be <?php } ?>.

    2) I forgot on the excerpt:

    add after $commentcount = $post->comment_count; this $excerpt = $post->post_excerpt;

    And replace <?php the_excerpt(); ?> with <?php echo $excerpt; ?>

    Its a miracle if works, the code is very bad assembled…

    Thread Starter udimagnet

    (@udimagnet)

    Hey,
    well, i’m not a php programmer – ive taken parts of codes from all over the web.. usually it works but this time… 🙁

    Here is my current code, after your changes. any chance there will be a miracle here?

    <?php $result = $wpdb->get_results("SELECT comment_count, ID, post_title, post_excerpt FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 3");
    							foreach ($result as $post) {
    							setup_postdata($post);
    							$postid = $post->ID;
    							$title = $post->post_title;
    							$commentcount = $post->comment_count;
    							$excerpt = $post->post_excerpt;
    							if ($commentcount != 0) { ?>
    
    					<div class="mostCommentedBox">
    							<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php
    						if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    						  the_post_thumbnail();
    						} else { ?>
    
    						<img src="<?php bloginfo('template_directory'); ?>/images/noimage.jpg">
    
    						<?php } ?> </a>
    						<strong><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></strong>
    						<div class="mostCommentedBoxCommetsCount" title="<?php echo $commentcount ?> comments"><?php echo $commentcount ?></div>
    							<p><?php echo $excerpt; ?></p>
    
    					</div>
    
    					<?php } } ?>

    Thanks so much

    The has_post_thumbnail must be in the loop. In the code, this break all.

    Try without the has_post_thumbnail function:

    <?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title,post_excerpt FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0,3");
    					foreach ($result as $post) {
    					setup_postdata($post);
    					$postid = $post->ID;
    					$title = $post->post_title;
    					$commentcount = $post->comment_count;
    					$excerpt = $post->post_excerpt;
    					if ($commentcount != 0) { ?>
                        	<div class="mostCommentedBox">
                                <strong><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></strong>
    						    <div class="mostCommentedBoxCommetsCount" title="<?php echo $commentcount ?> comments"><?php echo $commentcount ?></div>
    							<p><?php echo $excerpt; ?></p>
                            </div>
    			  <?php } } ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘showin the_excerpt in "most commented" posts’ is closed to new replies.