这篇wordpress教程主要是介绍非插件调用wordpress分类热评文章的调用代码,估计会有博主需要。操作方法也很简单,实现原理是通过读取某篇文章的评论数来调用相关的文章,其实准确来说应该是"热评文章"。
wordpress调用分类热评文章:
在wordpress当主题的functions.php文件的<?php和?>之间添加以下代码:
/* $termId:分类目录ID,为0时是检索所有分类目录 $posts_num:显示热评文章的数量 $days:检索多少天内的热评文章 */ // 获得热评文章 function simple_get_most_review($termId=0,$posts_num=10, $days=30) { global $wpdb; //所有热评文章 if($termId==0){ $sql = "SELECT `ID` , `post_title` , `comment_count` FROM $wpdb->posts WHERE `post_type` = 'post' AND TO_DAYS( now( ) ) - TO_DAYS( `post_date` ) < $days AND (`wp_posts`.`post_status` = 'publish' OR `wp_posts`.`post_status` = 'inherit') ORDER BY `comment_count` DESC LIMIT 0 , $posts_num "; } //分类热评文章 else { $sql="SELECT `ID` , `post_title` , `comment_count` FROM `wp_posts` INNER JOIN `wp_term_relationships` ON (`wp_posts`.`ID` = `wp_term_relationships`.`object_id`) INNER JOIN `wp_term_taxonomy` ON (`wp_term_relationships`.`term_taxonomy_id` = `wp_term_taxonomy`.`term_taxonomy_id`) WHERE 1=1 AND `wp_term_taxonomy`.`taxonomy` = 'category' AND `wp_term_taxonomy`.`term_id` = $termId AND `wp_posts`.`post_type` = 'post' AND (`wp_posts`.`post_status` = 'publish' OR `wp_posts`.`post_status` = 'inherit') GROUP BY `wp_posts`.`ID` ORDER BY `comment_count` DESC LIMIT 0 , 10 "; } $posts = $wpdb->get_results($sql); $output = ""; foreach ($posts as $post){ $overPost=$post->post_title; $output .= "\n<li><a href= \"".get_permalink($post->ID)."\" rel=\"bookmark\" title=\"".$post->post_title."\" >".$overPost."</a></li>"; } echo $output; }
以上代码包含了读取所有热评文章和分类热评文章,调用代码分别是
所有分类热评文章:
<?php if (function_exists('simple_get_most_review')) {simple_get_most_review(0,10,31); } ?>
单个分类文章热评:
<?php if (function_exists(''simple_get_most_review')) {simple_get_most_review($category->term_id,10,31); } ?>
把ID修改为分类ID。