【转】给wordpress添加文章发布时间的代码

建站 技术流评论2,913阅读模式

显示Wordpress文章发布时间,最后修改时间、评论数量的代码

文章源自原紫番博客-https://www.yuanzifan.com/53880.html

一、添加文章发布时间

一般发文章多了,加一个发布时间会显得很有条理,知道是什么时候什么时间写的文章(当然可以在后台查看,不过不方便)。下面给大家两种格式:文章源自原紫番博客-https://www.yuanzifan.com/53880.html

1、发布时间为XXX前

20160612110349在functions.php下添加如下代码:文章源自原紫番博客-https://www.yuanzifan.com/53880.html

function timeago( $ptime ) {
    $ptime = strtotime($ptime);
    $etime = time() - $ptime;
    if ($etime < 1) return '刚刚';
    $interval = array (
   12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
    30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
    7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
    24 * 60 * 60 => '天前',
    60 * 60 => '小时前',
    60 => '分钟前',
    1 => '秒前'
   );
    foreach ($interval as $secs => $str) {
    $d = $etime / $secs;
    if ($d >= 1) {
    $r = round($d);
    return $r . $str;
    }
   };
   }

列表页和文章页面: 使用的时候在需要显示时间的的地方加入以下代码即可:文章源自原紫番博客-https://www.yuanzifan.com/53880.html

<?php echo '发表于 '.timeago( get_gmt_from_date(get_the_time('Y-m-d G:i:s')) ); ?>

评论区域使用方法: 使用的时候在需要显示时间的的地方加入以下代码即可:文章源自原紫番博客-https://www.yuanzifan.com/53880.html

<?php echo '发表于 '.timeago( $comment->comment_date_gmt ); ?>

注意:此函数传值格式为“2016-06-06 11:11:11”,只要格式符合就行。文章源自原紫番博客-https://www.yuanzifan.com/53880.html

2、发布时间为具体的时间

20160612110141这种方法只需要用wordpress自带的函数即可,在需要显示发布时间的地方加入以下代码:文章源自原紫番博客-https://www.yuanzifan.com/53880.html

<?php echo the_time('Y-m-j h:s l'); ?>

wordpress时间函数以及参数

文章的时间函数为:<?php the_time() ?>
评论的时间函数为: <?php comment_date() ?>
评论的日期函数为:<?php comment_time() ?>

参数文章源自原紫番博客-https://www.yuanzifan.com/53880.html

参数描述文章源自原紫番博客-https://www.yuanzifan.com/53880.html

输出时间格式文章源自原紫番博客-https://www.yuanzifan.com/53880.html

d

日期

06

j

日期

6

D

星期

F

月份

一月

g

小时

6

G

小时

06

h

分钟

6

H

分钟

06

a

上下午

am/pm

A

上下午

AM/PM

l

星期

星期一

m

月份

01

M

月份

Jan

n

月份

6

O

时区

+0800

r

完整的日期时间

Mon, 06 Jan 2010 20:30:10 +0800

S

序列型数字的后缀

st/th

T

时区

CST

w

星期

2

W

周数

22

y

年份

10

Y

年份

2010

z

天数

365

下面再举几个WordPress设置时间格式的例子: 中文日期格式设置,年月日:如:2010年2月1日,则Wordpress the time函数的参数这样写:

<? php the_time('Y年n月j日'); ?>

中文时间的设置,小时分秒:如:22:22:22,则Wordpress the time函数的参数这样写:

<? php the_tim<? php the_time('G:i:s'); ?> e('Y年n月j日'); ?>

星期格式设置,星期:如:2010年11月1日星期四,则Wordpress the time函数的参数这样写:

<? php the_time('Y年n月j日l'); ?>

有时候我们主题喜欢中英文混输的日期显示格式,对于使用中文版 WordPress来说,这个就要做一些小小的调整了。这里以显示日志时间的月份为例,我们在主题中使用 the_time( 'M' ) 应该可以打印出一个 Sep 的英文简写的月份值,但是WordPress 会非常人性化地为你翻译成“九”,那么这个时候我们从函数角度出发解决此问题,重置一下,避免汉化。

将函数:

<?php the_time('M');?>

替换成:

<?php echo date('M',get_the_time('U'));?>

二、添加文章修改时间

wordpress自带修改时间函数the_modified_time(),这个函数就是显示最后更新时间用的。括号中还需要填上WordPress的日期格式。比如:

<?php the_modified_time('Y年n月j日'); ?>

其中的Y年n月j日可以自定义,比如改成Y-m-j或者Y-m-j h:s。格式同上面的发布日期的时间函数。

上边的标签太单一,最好能做个判断,如果文章修改过(不包括当天),则显示最后一次修改时间,没修改或者当天修改过,则显示发布日期,代码为:

 (get_the_time('Y')*365+get_the_time('z'))) : ?>
	最后修改:

	最后修改:

the_date_xml()是文章页模版的时间标签,如果在首页就需要修改成首页的。

三、添加文章评论数量

直接将下面的函数添加到当前主题的 functions.php

/* 获取文章的评论人数 by zwwooooo | zww.me */
function zfunc_comments_users($postid=0,$which=0) {
	$comments = get_comments('status=approve&type=comment&post_id='.$postid); //获取文章的所有评论
	if ($comments) {
		$i=0; $j=0; $commentusers=array();
		foreach ($comments as $comment) {
			++$i;
			if ($i==1) { $commentusers[] = $comment->comment_author_email; ++$j; }
			if ( !in_array($comment->comment_author_email, $commentusers) ) {
				$commentusers[] = $comment->comment_author_email;
				++$j;
			}
		}
		$output = array($j,$i);
		$which = ($which == 0) ? 0 : 1;
		return $output[$which]; //返回评论人数
	}
	return 0; //没有评论返回0
}

调用方法:

<?php echo zfunc_comments_users($postid); ?>

参数说明:$postid是需要获取评论人数的文章ID

一般用法:在一般主题的loop里面可以这样用:

<?php echo zfunc_comments_users($post->ID); ?>

PS:还可以输出评论总数,用法:

<?php echo zfunc_comments_users($postid, 1); ?>

原文链接:https://blog.csdn.net/xjtarzan/article/details/79268789

 最后更新:2019-7-12

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证