非插件调用wordpress热评文章的方法博客吧以前发布过《WordPress 博客非插件显示最多评论文章列表的方法》,而现在的这篇wordpress教程主要是介绍非插件调用wordpress分类热评文章的调用代码,估计会有博主需要。操作方法很简单,下面是博客吧找到的相关代码及详细的操作步骤。
wordpress调用分类热评文章:
在wordpress当主题的functions.php文件的<?php和?>之间添加以下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /* $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; } |
以上代码包含了读取所有热评文章和分类热评文章,调用代码分别是
所有分类热评文章:
1 | <?php if (function_exists('simple_get_most_review')) {simple_get_most_review(0,10,31); } ?> |
某分类热评文章:
1 | <?php if (function_exists(''simple_get_most_review')) {simple_get_most_review($category->term_id,10,31); } ?> |
把ID修改为分类ID
文章摘自盒子UI