这篇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。
Wordpress下载:
WordPress v4.4.2 英文版下载