get_day_link()
云策文档标注
概述
get_day_link() 函数用于获取指定年、月、日的归档页面固定链接。它支持参数默认值,并处理 WordPress 的永久链接结构。
关键要点
- 函数返回字符串类型的归档链接,基于年、月、日参数生成。
- 参数 $year、$month、$day 可接受整数或 false(false 时使用当前日期)。
- 内部使用 WP_Rewrite 处理永久链接结构,若未设置则回退到查询字符串格式。
- 通过 apply_filters('day_link', ...) 提供钩子,允许开发者过滤链接。
- 相关函数包括 zeroise()、current_time()、user_trailingslashit() 等。
代码示例
// 获取当前日期的归档链接
$link = get_day_link(false, false, false);
// 获取指定日期(如2023年12月25日)的归档链接
$link = get_day_link(2023, 12, 25);注意事项
- 确保参数为有效整数或 false,否则可能导致意外行为。
- 链接格式取决于 WordPress 的永久链接设置。
- 函数在循环中常与 get_the_time() 结合使用,以动态生成日期链接。
原文内容
Retrieves the permalink for the day archives with year and month.
Parameters
$yearint|falserequired-
Integer of year. False for current year.
$monthint|falserequired-
Integer of month. False for current month.
$dayint|falserequired-
Integer of day. False for current day.
Source
function get_day_link( $year, $month, $day ) {
global $wp_rewrite;
if ( ! $year ) {
$year = current_time( 'Y' );
}
if ( ! $month ) {
$month = current_time( 'm' );
}
if ( ! $day ) {
$day = current_time( 'j' );
}
$daylink = $wp_rewrite->get_day_permastruct();
if ( ! empty( $daylink ) ) {
$daylink = str_replace( '%year%', $year, $daylink );
$daylink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $daylink );
$daylink = str_replace( '%day%', zeroise( (int) $day, 2 ), $daylink );
$daylink = home_url( user_trailingslashit( $daylink, 'day' ) );
} else {
$daylink = home_url( '?m=' . $year . zeroise( $month, 2 ) . zeroise( $day, 2 ) );
}
/**
* Filters the day archive permalink.
*
* @since 1.5.0
*
* @param string $daylink Permalink for the day archive.
* @param int $year Year for the archive.
* @param int $month Month for the archive.
* @param int $day The day for the archive.
*/
return apply_filters( 'day_link', $daylink, $year, $month, $day );
}
Hooks
- apply_filters( ‘day_link’, string $daylink, int $year, int $month, int $day )
-
Filters the day archive permalink.
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
Skip to note 3 content
Codex
Use With Variables
PHP code block for use within The Loop: Assigns year, month and day of a post to the variables $arc_year, $arc_month and $arc_day. These are used with the get_day_link() tag, which returns the URL as a link to the daily archive for that post, displaying it within an anchor tag with the PHP echo command. See Formatting Date and Time for info on format strings used in get_the_time() tag.
<a href="<?php echo esc_url( get_day_link( $archive_year, $archive_month, $archive_day ) ); ?>"> </a>Skip to note 4 content
Codex
Current Day as Link
Returns the URL to the current day’s archive as a link by displaying it within an anchor tag with the PHP echo command.
<a href="<?php echo esc_url( get_day_link( false, false, false ) ); ?>"></a>