百度未收录

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

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

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

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

<?php   
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 ) . \'\" 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,否则中文会乱码。

二,用下面代码:

<?php echo my_entry_published_link(); ?>  

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

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

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

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

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

温馨提示: 本文最后更新于2022/10/20 04:00:48。若文章内容或图片失效,请 留言联系站长反馈!
!
也想出现在这里? 联系我们
创意广告
© 版权声明
THE END
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容