当我们的WordPress博客历史越来越悠久后,我们就会发现,在N年前的今天(也就是该篇文章所发表日期),我们也曾经发表了文章。而今天畅想资源就来教大家如何于WordPress文章内添加上「历史上的今天」功能,让站长和访客一同体会网站历史!
预览
PS:本文下方其实就有哦~
插件(插件)版
纯代码教学
将下方代码添加到「布景函式库 (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 45 46 47 |
/* 历史上的今天开始(由AREFLY.COM制作) */ 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'); /* 历史上的今天结束(由AREFLY.COM制作) */ |
历史上的今天
2013年:植物大战僵尸2限时免费啦(4条评论)