日期加时间才是完整的时间,前面已经讲了WordPress时间函数the_time的用法,这篇就讲与时间函数一起使用的WordPress日期函数the_date与get_the_date的用法,WordPress获取日期函数与获取时间函数的用法基本上一致,就连函数的构造都相差无几,下面我们一起来看看。
函数原型
与时间函数一样,两个函数都位于wp-includes/general-template.php文件中。
the_date:
复制
function the_date( $d = '', $before = '', $after = '', $echo = true ) {
global $currentday, $previousday;
if ( is_new_day() ) {
$the_date = $before . get_the_date( $d ) . $after;
$previousday = $currentday;
/**
* Filters the date a post was published for display.
*
* @since 0.71
*
* @param string $the_date The formatted date string.
* @param string $d PHP date format. Defaults to 'date_format' option
* if not specified.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_date = apply_filters( 'the_date', $the_date, $d, $before, $after );
if ( $echo )
echo $the_date;
else
return $the_date;
}
}get_the_date:
复制
function get_the_date( $d = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( '' == $d ) {
$the_date = mysql2date( get_option( 'date_format' ), $post->post_date );
} else {
$the_date = mysql2date( $d, $post->post_date );
}
/**
* Filters the date a post was published.
*
* @since 3.0.0
*
* @param string $the_date The formatted date.
* @param string $d PHP date format. Defaults to 'date_format' option
* if not specified.
* @param int|WP_Post $post The post object or ID.
*/
return apply_filters( 'get_the_date', $the_date, $d, $post );
}一样the_date调用了get_the_date方法,get_the_date方法里面查询了数据库。
函数描述
与时间函数一样,都是显示文章什么时候写的。
用法
复制
get_the_date( string $d = '', int|WP_Post $post = null )
参数
$d :格式化日期字符串。
$post:查询内容或id,可以为空。
简单使用
复制
<?php get_the_date('D M j')?>与时间函数一样,它也可以带格式化字符串参数。
循环输出文章日期
复制
<?php
.....
if(have_posts()){
.....
echo get_the_date('D M j',the_post());
....
}
.....
?>举个例子,没试过代码能不能正常运行。
the_date就和the_time差不多了,具体参数前面也发过了,详情请看:WordPress获取时间函数the_time与get_the_time详解。





评论 (0)