Forums

showin the_excerpt in "most commented" posts (6 posts)

  1. udimagnet
    Member
    Posted 1 year ago #

    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? :-)

  2. sdelamorena
    Member
    Posted 1 year ago #

    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!

  3. udimagnet
    Member
    Posted 1 year ago #

    Thanks for the reply but...Nope, still doesnt work... :-(

  4. sdelamorena
    Member
    Posted 1 year ago #

    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...

  5. udimagnet
    Member
    Posted 1 year ago #

    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

  6. sdelamorena
    Member
    Posted 1 year ago #

    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 } } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic