说明:前几天发了个无需插件给博客网站添加一言功能的方法,参考:给博客网站添加Hitokoto – 一言经典语句功能,现在再发个不需要插件给WordPress
网站添加“历史上的今天”功能的方法。这样可以很好清楚的知道,历史的同一天发过什么文章。
方法
在模板的function.php
最下面,也就是“?>
”之前添加下面代码即可实现:
function wp_history_post_base($post_year, $post_month, $post_day){
global $wpdb;
$limit = 30;
$order = "latest";
if($order == "latest"){ $order = "DESC";} else { $order = '';}
$sql = "select ID, year(post_date_gmt) as h_year, post_title, comment_count FROM
$wpdb->posts WHERE post_password = '' AND post_type = 'post' AND post_status = 'publish'
AND year(post_date_gmt)!='$post_year' AND month(post_date_gmt)='$post_month' AND day(post_date_gmt)='$post_day'
order by post_date_gmt $order limit $limit";
$histtory_post = $wpdb->get_results($sql);
return $histtory_post;
}
function wp_history_post_single(){
$wp_history_post_content_list = '<p>%YEAR%年:<a href="%LINK%" title="%TITLE%" rel="external nofollow">%TITLE%(%COMMENTS_NUM%条评论)</a></p>';
$wp_history_post_content_title = '<h3>历史上的今天</h3>';
$histtory_post = wp_history_post_base(get_the_time('Y'), get_the_time('m'), get_the_time('j'));
if($histtory_post){
foreach( $histtory_post as $post ){
$h_year = $post->h_year;
$h_post_title = $post->post_title;
$h_permalink = get_permalink( $post->ID );
$h_comments = $post->comment_count;
$h_post .= $wp_history_post_content_list;
$h_post = str_replace("%YEAR%", $h_year, $h_post);
$h_post = str_replace("%LINK%", $h_permalink, $h_post);
$h_post = str_replace("%TITLE%", $h_post_title, $h_post);
$h_post = str_replace("%COMMENTS_NUM%", $h_comments, $h_post);
}
}
if($h_post){
$result = $wp_history_post_content_title.$h_post;
}
return $result;
wp_reset_query();
}
function wp_history_post_content($content){
global $wpdb;
if(is_single()){
$content .= wp_history_post_single();
}
return $content;
}
add_action('the_content', 'wp_history_post_content');
未经允许不得转载:嘟嘟 » 给WordPress博客网站添加“历史上的今天”功能
最新评论
请问下,python3 tg_username_update.py都会提示下面这个是因为什么啊 Traceback (most recent call last): File "tg_username_update.py", line 21, in <module> cake = emojize(":cake:", use_aliases=True) TypeError: emojize() got an unexpected keyword argument 'use_aliases'
不可以了
# Install [Debian] [bullseye] [amd64] Downloading... grep: (standard input): binary file matches Error! grub.cfg. root@RRR2:~#
博主你好,请问cloudfront 上传收费这个是事实吧,已经找了几个贴子确认了。 那有什么好办法能够避开这个收费呢?如果不能避开感觉还是cloudflare 比较稳。
来看看
第5步添加自定义主机名进去后等txt验证提示custom hostname does not name to this zone
那没事了,我静态博客。谢谢
为什么缓存键和源请求那里不设置缓存?