×

WordPress获取日期:页面上显示四件的函数the_date与get_the_date解析

作者:Terry2024.05.15来源:Web前端之家浏览:674评论:0
关键词:WordPress

分享一个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>


您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/kaifagongju8029384.html

网友评论文明上网理性发言 已有0人参与

发表评论: