WordPress获取日期函数the_date与get_the_date解析并且如何修改文章显示的时间

函数原型:

两个函数都位于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方法里面查询了数据库。

函数描述:都是输出时间

the_date函数用法:

<?php the_date( $format, $before, $after, $echo ); ?>

1.默认用法
使用默认值显示日期。
<p>Date posted: <?php the_date(); ?></p>

2.日期为标题中的年、月、日
使用<h2>标记内的“2007-07-23”格式(例如:2004-11-30)显示日期。
<?php the_date('Y-m-d', '<h2>', '</h2>'); ?>

3.使用$my_Date变量输入标题中的日期
以<h2>标记内的默认格式返回日期,并将其分配给$my_date变量。然后用PHP echo命令显示变量的值。
<?php $my_date = the_date('', '<h2>', '</h2>', FALSE); echo $my_date; ?>

get_the_date函数用法:

<?php $pfx_date = get_the_date( $d ); ?>

1.默认用法
<span class="entry-date"><?php echo get_the_date(); ?></span>

2.日期为标题中的年、月、日
<span class="entry-date"><?php echo get_the_date('Y-m-d'); ?></span>

题外话:博主用wordpress建了一个自己的博客,感兴趣的朋友都可以来围观 吃饭用大碗的程序猿

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值