有一些文章实时比较强,我们可以给文章加上最后更新时间 ,一来有利SEO,二来有利来读者知道文章内容是否还是有效的,代码如下:
1、直接在主题中替换代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // 注意<?php the_date_xml(); ?>是文章页模版的时间标签,如果在首页就需要修改成首页的 <?php if ((get_the_modified_time( 'Y' )*365+get_the_modified_time( 'z' )) > (get_the_time( 'Y' )*365+get_the_time( 'z' ))) : ?>最后修改: <?php the_modified_time( 'Y年m月j日 h:s' ); ?> <?php else : ?> <?php the_date_xml(); ?> <?php endif ; ?> // 挖站否正在用的 <?php if ((get_the_modified_time( 'Y' )*365+get_the_modified_time( 'z' )) > (get_the_time( 'Y' )*365+get_the_time( 'z' ))) : ?><li><li><i class = "fa fa-pencil-square" ></i><time class = "updated" datetime= "<?php the_modified_time('Y年m月j日 h:s'); ?>" >更新: <?php the_modified_time( 'Y年m月j日 h:s' ); ?></time></li> <?php endif ; ?> //简洁版本的 <?php if ((get_the_modified_time( 'Y' )*365+get_the_modified_time( 'z' )) > (get_the_time( 'Y' )*365+get_the_time( 'z' ))) : ?><li><li><i class = "fa fa-pencil-square" ></i>更新: <?php the_modified_time( 'Y年m月j日 h:s' ); ?></li> <?php endif ; ?> |
2、Functions.php中添加函数,然后在每篇文章前面插入最后更新时间信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //在每篇文章前面插入最后更新时间 function wpb_last_updated_date( $content ) { $u_time = get_the_time( 'U' ); $u_modified_time = get_the_modified_time( 'U' ); $custom_content = '' ; if ( $u_modified_time >= $u_time + 86400) { $updated_date = get_the_modified_time( 'Y年n月j日' ); $updated_time = get_the_modified_time( 'H:i' ); $custom_content .= '<p>更新: ' . $updated_date . $updated_time . '</p>' ; } $custom_content .= $content ; return $custom_content ; } add_filter( 'the_content' , 'wpb_last_updated_date' ); |
Qi 问的问题 2019年9月22日