在begin主题或者其他收费主题中,作者都代码集成了历史上的今天这样一个小功能。通过在文末显示一年前的文章。
其实也可以通过wp-today插件实现这个功能,但是wp-today插件作者柳城博客已经不再更新这个插件了。这个插件比较小,虽然是非常久远的插件,但是使用起来还是非常不错的。
这个WordPress插件对内链的优化相当不错的。所以还是非常值得拥有的。
[ad]
不过前提是文章要比较多,更新时间要比较长,最短也要一年以上。不知道是否可以实现一个月前的今天的文章功能。。这样的话,也是非常不错的。
文章部分内容是直接摘取的网友分享的两段代码。
其中一个是boke112提供的代码:
//历史上的今天 WordPress百宝箱 https://zhuji.gd/ function wp_today(){ global $wpdb; $post_year = get_the_time('Y'); $post_month = get_the_time('m'); $post_day = get_the_time('j'); $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 DESC limit 5"; $histtory_post = $wpdb->get_results($sql); 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 .= "<li><strong>$h_year:</strong> <a href='".$h_permalink."' title='".$h_post_title."' target='_blank'>$h_post_title($h_comments)</a></li>"; } } if ( $h_post ){ $result = "<h2>历史上的今天:</h2><ul>".$h_post."</ul>"; } return $result; } function wp_today_auto($content){ if( is_single() ){ $content = $content.wp_today(); } return $content; } add_filter('the_content', 'wp_today_auto',9999);
提示:以上代码默认是将历史上的今天添加到文章的最后,如果需要人工设置位置,只需要将29-35行的代码删除,然后在指定位置添加以下代码即可:
<?php echo wp_today(); ?>
第二段是从 Rat's Blog分享过来的。
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博客实现历史上的今天的小功能