• Hey guys!

    The theme has its own Top Articles sidebar next to the featured section, its written into header.php. Its suppose to display articles with most comments, but it doesn’t – instead, it displays the most recent articles including DRAFTS, that are not even published. If you click that it obviously gives the 404 error, because there is no article published yet.

    Can anyone help? Here is the code for Top Articles in header.php

    <div class="top">
    					<span class="heading1"><span>Top Articles</span></span>
    					<ul>
    					<?php
                                            $kt=time()-1209600;
    						$sql = "SELECT * FROM $wpdb->posts WHERE post_date_gmt > FROM_UNIXTIME(".$kt.") AND post_type = 'post' 	ORDER BY comment_count DESC LIMIT 5";
    						$top_posts = $wpdb->get_results($sql);
    
    						foreach ($top_posts as $post)  :
    							?>
    							<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span><a class="comments" href="<?php the_permalink(); ?>#comments"><?php echo $post->comment_count; ?> Comments</a>Posted Under: <?php the_category(', '); ?></span></li>
                                                            <?php
    						endforeach;
    					?>
    					</ul>
    				</div>
    				<div class="clear"></div>
    			</div>
Viewing 3 replies - 1 through 3 (of 3 total)
  • If this is supposed to be the top 5 commented posts, irregardless of date posted…
    Try changing;
    $sql = "SELECT * FROM $wpdb->posts WHERE post_date_gmt > FROM_UNIXTIME(".$kt.") AND post_type = 'post' ORDER BY comment_count DESC LIMIT 5";

    To;
    $sql = "SELECT * FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' ORDER BY comment_count DESC LIMIT 5";

    The key is selecting published posts, rather than all.

    There is probably a better way to do this than hitting the DB directly. Maybe someone else knows a built-in function to do this.

    Don

    Thread Starter TFolder

    (@tfolder)

    Awesome, worked like a charm! Thanks!

    You’re welcome.
    Glad I could help.

    Don

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Top Articles code, please look’ is closed to new replies.