钩子文档

get_the_date

💡 云策文档标注

概述

get_the_date 是一个 WordPress 过滤器,用于修改文章日期的输出格式。它允许开发者通过钩子自定义日期显示,例如针对特定文章类型调整日期格式。

关键要点

  • 过滤器名称:get_the_date
  • 参数:$the_date(格式化日期字符串或 Unix 时间戳)、$format(PHP 日期格式)、$post(文章对象)
  • 用途:过滤文章日期,常用于自定义日期显示逻辑
  • 相关函数:get_the_date()

代码示例

add_filter( 'get_the_date', 'wpdocs_filter_publish_dates', 10, 3 );

function wpdocs_filter_publish_dates( $the_date, $d, $post ) {
    if ( is_int( $post) ) {
        $post_id = $post;
    } else {
        $post_id = $post->ID;
    }

    if ( 'product' != get_post_type( $post_id ) )
        return $the_date;

    return date( 'Y-d-m - h:j:s', strtotime( $the_date ) );
}

📄 原文内容

Filters the date of the post.

Parameters

$the_datestring|int
Formatted date string or Unix timestamp if $format is 'U' or 'G'.
$formatstring
PHP date format.
$postWP_Post
The post object.

Source

return apply_filters( 'get_the_date', $the_date, $format, $post );

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    Below is a basic example on how to use the get_the_date filter to modify date format for a post type called ‘product’:

    add_filter( 'get_the_date', 'wpdocs_filter_publish_dates', 10, 3 );
    
    function wpdocs_filter_publish_dates( $the_date, $d, $post ) {
    	if ( is_int( $post) ) {
    		$post_id = $post;
    	} else {
    		$post_id = $post->ID;
    	}
    
    	if ( 'product' != get_post_type( $post_id ) )
    		return $the_date;
    
    	return date( 'Y-d-m - h:j:s', strtotime( $the_date ) );
    }