将日志发布日期链接到对应的归档页面

quality,Q 70

1638704612 20211205114332 61aca5e46d4aa

大多数情况下WordPress博客为了更多地展示精彩文章,会在侧边小工具中添加一个基于日期的文章归档列表,不过这样不仅占用了侧边大块的空间,而且也不是很美观。下面的方法可以在不占用任何额外的空间的前提下,方便读者按年,月,日阅览日志文章,增加流量。

一般的WordPress主题都会在显著的位置注明日志的发布或者修改日期,并没有什么实用价值,我们的目的就是将年,月,日连接到相应的存档页面。

一,将下面的代码添加到主题functions.php模版的最后:

  1. <?php   
  2. add_shortcode( ‘entry-link-published’, ‘my_entry_published_link’ );   
  3. function my_entry_published_link() {   
  4.     /* 获取当前日志的年,月,日. */  
  5.     $year = get_the_time( ‘Y’ );   
  6.     $month = get_the_time( ‘m’ );   
  7.     $day = get_the_time( ‘d’ );   
  8.     $out = ;   
  9.     /* 添加链接到年存档. */  
  10.     $out .= ‘<a href=“‘ . get_year_link( $year ) . ‘” title=“查看所有’ . esc_attr( $year ) . ‘年文章”>’ . $year . ‘年</a>’;   
  11.     /* 添加链接到月存档. */  
  12.     $out .= ‘<a href=“‘ . get_month_link( $year, $month ) . ‘” title=“查看所有’ . esc_attr( get_the_time( ‘Y年m月’ ) ) . ‘文章”>’ . get_the_time( ‘m月’ ) . ‘</a>’;   
  13.     /* 添加链接到日存档. */  
  14.     $out .= ‘<a href=“‘ . get_day_link( $year, $month, $day ) . ‘” title=“查看所有’ . esc_attr( get_the_time( ‘Y年m月d日’ ) ) . ‘文章”>’ . $day . ‘日</a>’;   
  15.     return $out;   
  16. }   
  17. ?>  

由于代码中有中文,记得将functions.php模版编码修改为:UTF-8 无BOM,否则中文会乱码。

二,用下面代码:

  1. <?php echo my_entry_published_link(); ?>  

替换主题模版默认时间函数:

  1. <?php the_time(‘Y年m月d日’) ?>  

包括:首页模版:index、文章页面模版:single、分类归档模版:archive、搜索结果模版:search等

替换完成后,可以分别点击日志发布日期的年,月,日,会打开相应的存档页面。

具体效果可以点击查看本博日志标题下的日期。

原文:Linking post published dates to their archives