函数文档

the_weekday_date()

💡 云策文档标注

概述

the_weekday_date() 是一个 WordPress 模板函数,用于显示文章的本地化星期几。它仅在当前文章的星期几与之前输出的不同时才输出,以避免重复显示。

关键要点

  • 函数输出文章的本地化星期几,基于文章发布时间计算。
  • 接受两个可选参数:$before(输出前添加的字符串)和 $after(输出后添加的字符串),默认均为空。
  • 使用全局变量 $wp_locale、$currentday 和 $previousweekday 来跟踪和比较星期几。
  • 包含一个过滤器钩子 apply_filters('the_weekday_date', ...),允许开发者自定义输出。
  • 相关函数包括 get_post_time()、WP_Locale::get_weekday()、apply_filters() 和 get_post()。

代码示例

function the_weekday_date( $before = '', $after = '' ) {
	global $wp_locale, $currentday, $previousweekday;

	$post = get_post();

	if ( ! $post ) {
		return;
	}

	$the_weekday_date = '';

	if ( $currentday !== $previousweekday ) {
		$the_weekday_date .= $before;
		$the_weekday_date .= $wp_locale->get_weekday( get_post_time( 'w', false, $post ) );
		$the_weekday_date .= $after;
		$previousweekday   = $currentday;
	}

	echo apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
}

注意事项

  • 函数直接输出结果,无返回值,适用于模板文件中直接显示。
  • 依赖于全局变量,确保在正确上下文中调用以避免意外行为。
  • 自 WordPress 0.71 版本引入,保持向后兼容性。

📄 原文内容

Displays the localized weekday for the post.

Description

Will only output the weekday if the current post’s weekday is different from the previous one output.

Parameters

$beforestringoptional
Output before the date. Default empty.
$afterstringoptional
Output after the date. Default empty.

Source

function the_weekday_date( $before = '', $after = '' ) {
	global $wp_locale, $currentday, $previousweekday;

	$post = get_post();

	if ( ! $post ) {
		return;
	}

	$the_weekday_date = '';

	if ( $currentday !== $previousweekday ) {
		$the_weekday_date .= $before;
		$the_weekday_date .= $wp_locale->get_weekday( get_post_time( 'w', false, $post ) );
		$the_weekday_date .= $after;
		$previousweekday   = $currentday;
	}

	/**
	 * Filters the localized weekday of the post, for display.
	 *
	 * @since 0.71
	 *
	 * @param string $the_weekday_date The weekday on which the post was written.
	 * @param string $before           The HTML to output before the date.
	 * @param string $after            The HTML to output after the date.
	 */
	echo apply_filters( 'the_weekday_date', $the_weekday_date, $before, $after );
}

Hooks

apply_filters( ‘the_weekday_date’, string $the_weekday_date, string $before, string $after )

Filters the localized weekday of the post, for display.

Changelog

Version Description
0.71 Introduced.