目前 WordPress 顯示熱門文章採用的是呼叫熱評文章的模式,雖然很多人傾向於呼叫瀏覽最多的文章,但是隻靠 WordPress 還無法實現 (當然藉助外掛還是可以實現的) 。然後呼叫熱評文章,在網上流行的程式碼區別還是較大的。最主要區別在於是否會出現 「友情連結」 這樣一項,因為一旦你的網站和我的網站一樣,有申請友情連結的留言,就有可能出現這樣的顯示,這對於使用者來說是很不友好的。為此區別,我把兩段程式碼都貼出來:
第一段會顯示 WordPress 會顯示 「友情連結」:
「
<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM
$wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10″);
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>
<li><a href=”<?php echo get_permalink($postid); ?>” title=”<?php echo
$title ?>”><?php echo $title ?></a></li>
<?php } } ?>
」
第二段 WordPress 不會顯示」 友情連結」
「
<?php
$post_num = 10; // 設定呼叫條數
$args = array(
‘post_password’ => ”,
‘post_status’ => ‘publish’, // 只選公開的文章.
‘post__not_in’ => array($post->ID),//排除當前文章.
‘caller_get_posts’ => 1, // 排除置頂文章.
‘orderby’ => ‘comment_count’, // 依評論數排序.
‘posts_per_page’ => $post_num
);
$query_posts = new WP_Query();
$query_posts->query($args);
while($query_posts->have_posts()){$query_posts->the_post(); ?>
<li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
<?php } wp_reset_query();?>
」
以上程式碼均經過除錯沒有問題。網上有些程式碼不可用,主要是全形半形的問題或缺少逗號和分號的原因。