大多数情况下WordPRess博客为了更多地展示精彩文章,会在侧边小工具中添加一个基于日期的文章归档列表,不过这样不仅占用了侧边大块的空间,而且也不是很美观。下面的方法可以在不占用任何额外的空间的前提下,方便读者按年,月,日阅览日志文章,增加流量。
一般的WordPress主题都会在显著的位置注明日志的发布或者修改日期,并没有什么实用价值,我们的目的就是将年,月,日连接到相应的存档页面。
一,将下面的代码添加到主题functions.php模版的最后:
<?phpadd_shortcode('entry-link-published','my_entry_published_link');functionmy_entry_published_link(){/* 获取当前日志的年,月,日. */$year=get_the_time('Y');$month=get_the_time('m');$day=get_the_time('d');$out='';/* 添加链接到年存档. */$out.='<a href="'.get_year_link($year).'" title="查看所有'.esc_attr($year).'年文章">'.$year.'年</a>';/* 添加链接到月存档. */$out.='<a href="'.get_month_link($year,$month).'" title="查看所有'.esc_attr(get_the_time('Y年m月')).'文章">'.get_the_time('m月').'</a>';/* 添加链接到日存档. */$out.='<a href="'.get_day_link($year,$month,$day).'" title="查看所有'.esc_attr(get_the_time('Y年m月d日')).'文章">'.$day.'日</a>';return$out;}?>
由于代码中有中文,记得将functions.php模版编码修改为:UTF-8 无BOM,否则中文会乱码。
二,用下面代码:
<?phpechomy_entry_published_link();?>
替换主题模版默认时间函数:
<?phpthe_time('Y年m月d日')?>
包括:首页模版:index、文章页面模版:single、分类归档模版:archive、搜索结果模版:search等。
替换完成后,可以分别点击日志发布日期的年,月,日,会打开相应的存档页面。
原文地址:http://zmingcx.com/date-link-to-the-archive.html