小赖子的英国生活和资讯

为博客WordPress添加两个显示十大文章的短指令 shortcode

阅读 桌面完整版

每年年底看着很多博主都会列出今年最佳十大文章啥的作一个总结,这事以前我也干过,不过以前是登陆服务器,连接数据库,然后跑几个SQL指令,得到结果,然后拷贝到文章中,很麻烦,而且这结果还是当下的,当SQL跑出来后结果就静态了。

其实,可以通过 shortcode 短指令的方式把这个功能添加到 wordpress 博客中。

WordPress 短指令:获得十大评论最多的文章

这个短指令有几个参数可以配置:

以下就是获得十大评论最多的文章的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年十大评论最高的文章。

  1. 拍照或摄影中采用的"男友视角"是什么
  2. 2023年生日: 快乐的时间很快就过去了
  3. 2023年年终总结: 多和优秀的人在一起玩
  4. 剑桥新开了家中餐小点心 Your Dumplings 豆浆和生煎
  5. 虚拟货币USDT兑换法币英镑的汇率比较: Crypto.com, Ledger硬件钱包卡 和 WirexApps
  6. iPhone 15 Pro Max 1TB 体验: 手机容量进入TB时代
  7. 焦虑, 失眠, 梦游
  8. Windows提高系统运行速度最简单粗暴的方法
  9. 整合 ChatGPT 到微信公众号机器人
  10. Amazon Photos 无限量照片备份

请记住,如果您不指定“年”参数,则将返回有史以来十大帖子。

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年十大评分文章:

  1. 4个升级CloudFlare免费到Pro套餐的理由
  2. 2023年年终总结: 多和优秀的人在一起玩
  3. 出国20年, 英国物价翻了好几番
  4. "躺平"生日蛋糕, 祝媳妇早日躺平
  5. 海外漂泊一晃二十年
  6. 如何解决微博视频下载出现的403错误(Denied by Referer ACL)?
  7. 老大英国小学毕业了
  8. 互联网大厂的黑客马拉松/Hackathon简介
  9. 剑桥很有特色的北非餐厅 Bedouin (好吃便宜)
  10. 儿子问我软件工程师的工作体验是怎么样的?

Wordpress博文统计

Wordpress博客技术文章

英文:Adding Two Short Code Functions to WordPress: Top Posts By Number of Comments and Top Posts by Ratings

强烈推荐

微信公众号: 小赖子的英国生活和资讯 JustYYUK

阅读 桌面完整版
Exit mobile version