WordPress文章发布时间加上链接

相信大家的WordPress博客在发布文章的时候,都会在文章标题或者结尾部分注明发布时间(更新日期)。然而并没有发挥什么作用,耕博在这里把博客发布的日志的时间链接到对应的文章归档。

操作方法步骤:

1、打开当前wp主题文件夹中的functions.php文件,使用utf-8编码添加如下代码:

//日志发布日期链接到存档   
add_shortcode( 'entry-link-published', 'my_entry_published_link' );      
function my_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 ) . '" rel="nofollow" title="查看所有' . esc_attr( $year ) . '年文章">' . $year . '年</a>';      
/* 添加链接到月存档. */     
$out .= '<a href="' . get_month_link( $year, $month ) . '" rel="nofollow" title="查看所有' . esc_attr( get_the_time( 'Y年m月' ) ) . '文章">' . get_the_time( 'm月' ) . '</a>';      
/* 添加链接到日存档. */     
$out .= '<a href="' . get_day_link( $year, $month, $day ) . '" rel="nofollow" title="查看所有' . esc_attr( get_the_time( 'Y年m月d日' ) ) . '文章">' . $day . '日</a>';      
return $out;      
}

2、用下面的代码,替换当前主题默认时间函数【一般在文章页single.php,分类文档archive.php ,包括首页index.php等】

<?php echo my_entry_published_link(); ?>

例:梦飞扬用的上面的代码,替换掉主题默认的时间函数

<?php the_time('Y年m月d日') ?>

大功告成,现在可以分别点击文章发布日期的年、月、日,打开对应的日期存档。演示效果可查看本文标题下的日期。

展开评论