每年年底看着很多博主都会列出今年最佳十大文章啥的作一个总结,这事以前我也干过,不过以前是登陆服务器,连接数据库,然后跑几个SQL指令,得到结果,然后拷贝到文章中,很麻烦,而且这结果还是当下的,当SQL跑出来后结果就静态了。
其实,可以通过 shortcode 短指令的方式把这个功能添加到 wordpress 博客中。
WordPress 短指令:获得十大评论最多的文章
这个短指令有几个参数可以配置:
- “year”:帖子年份,可选,如果没有指定年份,则是基于当年博客中的所有文章来统计。
- “type”:这个是HTML列表的类型,默认是 ol(ordered list) 也可以选择 ul(unordered list)
- “urltype”:博客链接的方式(默认是short),可以是 full 也可以是 short。这里提供了两种方式,但实际上你可以稍微改一下代码自定义 permlink 的格式。
- “count”:返回帖子的数目,默认是10个。
以下就是获得十大评论最多的文章的wordpress shortcode PHP函数,您需要复制添加到主题的 functions.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 41 42 43 44 | function show_top_posts_by_comments_func($atts) { global $wpdb; extract(shortcode_atts( array( 'year' => '', 'type' => 'ol', 'urltype' => 'short', 'count' => '10' ), $atts)); $query = " SELECT `id`, `post_name`, `post_title` FROM `$wpdb->posts` WHERE `post_type` = 'post' and `post_status` = 'publish' "; if ($year) { $query .= " and date_format(`post_date_gmt`, \"%Y\") = '$year' "; } $query .= " ORDER BY `comment_count` DESC LIMIT $count "; $result = $wpdb->get_results($query); $ans = "<$type>"; if ($result) { foreach ($result as $post) { if ($urltype === "short") { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/archives/".($post->id)."'>".$post->post_title."</a></li>"; } else { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/". $post->post_name . "/'>".$post->post_title."</a></li>"; } } } $ans .= "</$type>"; return $ans; } add_shortcode('show_top_posts_by_comments', 'show_top_posts_by_comments_func'); |
function show_top_posts_by_comments_func($atts) { global $wpdb; extract(shortcode_atts( array( 'year' => '', 'type' => 'ol', 'urltype' => 'short', 'count' => '10' ), $atts)); $query = " SELECT `id`, `post_name`, `post_title` FROM `$wpdb->posts` WHERE `post_type` = 'post' and `post_status` = 'publish' "; if ($year) { $query .= " and date_format(`post_date_gmt`, \"%Y\") = '$year' "; } $query .= " ORDER BY `comment_count` DESC LIMIT $count "; $result = $wpdb->get_results($query); $ans = "<$type>"; if ($result) { foreach ($result as $post) { if ($urltype === "short") { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/archives/".($post->id)."'>".$post->post_title."</a></li>"; } else { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/". $post->post_name . "/'>".$post->post_title."</a></li>"; } } } $ans .= "</$type>"; return $ans; } add_shortcode('show_top_posts_by_comments', 'show_top_posts_by_comments_func');
在Wordpress文章或者页面中使用,只需要这么添加:
1 2 | // 在前面添加 [,在末尾添加 ] show_top_posts_by_comments urltype="short" count="10" year="2023" type="ol" |
// 在前面添加 [,在末尾添加 ] show_top_posts_by_comments urltype="short" count="10" year="2023" type="ol"
实时效果,以下显示2023年十大评论最高的文章。
- 拍照或摄影中采用的"男友视角"是什么?
- 2023年生日: 快乐的时间很快就过去了
- 2023年年终总结: 多和优秀的人在一起玩
- 虚拟货币USDT兑换法币英镑的汇率比较: Crypto.com, Ledger硬件钱包卡 和 WirexApps
- 剑桥新开了家中餐小点心 Your Dumplings 豆浆和生煎
- 花钱让人换汽车钥匙的电池真是个智商税
- 焦虑, 失眠, 梦游
- iPhone 15 Pro Max 1TB 体验: 手机容量进入TB时代
- 整合 ChatGPT 到微信公众号机器人
- Windows提高系统运行速度最简单粗暴的方法
请记住,如果您不指定“年”参数,则将返回有史以来十大帖子。
WordPress 短指令:获得十大评分最高的文章
同样,您可以调用短代码show_top_posts_by_rating通过评分最高的十大帖子。 此功能具有相同的参数/用法,因此请参见上面的示例。
把下面的PHP代码添加到 functions.php 用于添加获得十大评分最高的文章的 wordpress shortcode 函数。
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 41 42 43 44 45 46 47 48 49 50 | function show_top_posts_by_rating_func($atts) { global $wpdb; extract(shortcode_atts( array( 'year' => '', 'type' => 'ol', 'urltype' => 'short', 'count' => '10' ), $atts)); $query = " SELECT `p`.`ID` as `id`, `p`.`post_title` as `post_title`, `p`.`post_name` as `post_name`, `visitor_votes` + `user_votes` as `total_votes`, `visitor_votes`, `user_votes` FROM `".$wpdb->prefix."gdsr_data_article` as `da` INNER JOIN `$wpdb->posts` as `p` ON `da`.`post_id` = `p`.`ID` WHERE `p`.`post_type` = 'post' and `p`.`post_status` = 'publish' "; if ($year) { $query .= " and date_format(`post_date_gmt`, \"%Y\") = '$year' "; } $query .= " HAVING `total_votes` > 0 ORDER BY `total_votes` DESC LIMIT $count "; $result = $wpdb->get_results($query); $ans = "<$type>"; if ($result) { foreach ($result as $post) { if ($urltype === "short") { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/archives/".($post->id)."'>".$post->post_title."</a></li>"; } else { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/". $post->post_name . "/'>".$post->post_title."</a></li>"; } } } $ans .= "</$type>"; return $ans; } add_shortcode('show_top_posts_by_rating', 'show_top_posts_by_rating_func'); |
function show_top_posts_by_rating_func($atts) { global $wpdb; extract(shortcode_atts( array( 'year' => '', 'type' => 'ol', 'urltype' => 'short', 'count' => '10' ), $atts)); $query = " SELECT `p`.`ID` as `id`, `p`.`post_title` as `post_title`, `p`.`post_name` as `post_name`, `visitor_votes` + `user_votes` as `total_votes`, `visitor_votes`, `user_votes` FROM `".$wpdb->prefix."gdsr_data_article` as `da` INNER JOIN `$wpdb->posts` as `p` ON `da`.`post_id` = `p`.`ID` WHERE `p`.`post_type` = 'post' and `p`.`post_status` = 'publish' "; if ($year) { $query .= " and date_format(`post_date_gmt`, \"%Y\") = '$year' "; } $query .= " HAVING `total_votes` > 0 ORDER BY `total_votes` DESC LIMIT $count "; $result = $wpdb->get_results($query); $ans = "<$type>"; if ($result) { foreach ($result as $post) { if ($urltype === "short") { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/archives/".($post->id)."'>".$post->post_title."</a></li>"; } else { $ans .= "<li><a title='".htmlentities($result->title, ENT_QUOTES)."' href='/". $post->post_name . "/'>".$post->post_title."</a></li>"; } } } $ans .= "</$type>"; return $ans; } add_shortcode('show_top_posts_by_rating', 'show_top_posts_by_rating_func');
使用例子:
1 2 | // 在前面添加 [,在末尾添加 ] show_top_posts_by_rating urltype="short" count="10" year="2023" type="ol" |
// 在前面添加 [,在末尾添加 ] show_top_posts_by_rating urltype="short" count="10" year="2023" type="ol"
实时效果,显示2023年十大评分文章:
- 4个升级CloudFlare免费到Pro套餐的理由
- 2023年年终总结: 多和优秀的人在一起玩
- 出国20年, 英国物价翻了好几番
- "躺平"生日蛋糕, 祝媳妇早日躺平
- 海外漂泊一晃二十年
- 如何解决微博视频下载出现的403错误(Denied by Referer ACL)?
- 老大英国小学毕业了
- 互联网大厂的黑客马拉松/Hackathon简介
- 剑桥很有特色的北非餐厅 Bedouin (好吃便宜)
- 儿子问我软件工程师的工作体验是怎么样的?
Wordpress博文统计
- 为博客Wordpress添加两个显示十大文章的短指令 shortcode
- 如何通过SQL语句找出得分最多的文章? (2016 年博文统计)
- 通过SQL查询2015年最受欢迎的博文
- 2020年最受欢迎的博文统计
- 2019年最受欢迎的博文统计
- 2017 年, 我的热门帖子 My Top Posts in 2017!
- 按评论排名前20的文章
- 按评分排名前20的文章
Wordpress博客技术文章
- 为博客Wordpress添加两个显示十大文章的短指令 shortcode
- 添加短代码(Short Code Function)以在 WordPress 帖子或页面中包含任何 PHP 或 HTML 文件
- 给Wordpress提了个BUG得到了一件免费的T-Shirt
- 最简单有效的过滤Wordpress垃圾评论的方法
- 怎么样对你的Wordpress博客进行汉化?
- 怎么样移动Wordpress博客的评论?
- Wordpress 博客使用 AMP 移动加速的技术和技巧
- 怎么样正确的统计WORDPRESS博文的汉字个数?
- 本人提供有偿 WORDPRESS或相关网站 速度SEO优化等服务
- 如何把 隐藏 WORDPRESS 的顶部管理菜单?
- 通过PHP脚本 批量设置 WORDPRESS 博客文件夹属性
- 在WORDPRESS管理员界面上面添加菜单选项
- 开发 Wordpress 插件 教程 - 插件是如何工作的?
- Wordpress 最简单的过滤垃圾评论的方法
- 如何显示缩略图 随机文章 Wordpress?
- 如何在文章最后显示 历史上的今天 [Wordpress]?
- 登陆 Wordpress 之后不显示Adsense广告
- 如何在指定的文章里不显示Adsense广告? Adsense真是矫情
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK