函数文档

get_month_link()

💡 云策文档标注

概述

get_month_link() 函数用于获取指定年份和月份的归档页面固定链接。它支持自定义永久链接结构,并可通过过滤器进行修改。

关键要点

  • 函数接受两个参数:$year(年份整数,false 表示当前年份)和 $month(月份整数,false 表示当前月份)。
  • 返回指定月份归档的固定链接字符串。
  • 内部处理逻辑:如果未提供年份或月份,则使用 current_time() 获取当前值;根据 WP_Rewrite 的永久链接结构生成链接,否则使用查询字符串格式。
  • 应用了 'month_link' 过滤器,允许开发者自定义链接输出。
  • 使用时机:最早可在 setup_theme Action 中使用,更早使用(如 plugins_loaded)会导致致命错误。
  • 相关函数包括 zeroise()、current_time()、user_trailingslashit()、home_url() 等。

代码示例

// 获取当前月份归档链接
$current_month_link = get_month_link( false, false );

// 获取特定月份归档链接(例如 2004 年 10 月)
$oct_04_link = get_month_link( 2004, 10 );

// 在循环中使用,基于文章日期生成链接
$arc_year = get_the_time( 'Y' );
$arc_month = get_the_time( 'm' );
$archive_link = get_month_link( $arc_year, $arc_month );
echo '<a href="' . esc_url( $archive_link ) . '">Archive for ' . get_the_time( 'F Y' ) . '</a>';

注意事项

  • 确保在 setup_theme Action 或之后调用,避免早期使用导致错误。
  • 参数应为整数或 false,月份会自动补零(如 1 变为 01)。
  • 链接格式取决于站点的永久链接设置,可能为结构化或查询字符串形式。

📄 原文内容

Retrieves the permalink for the month archives with year.

Parameters

$yearint|falserequired
Integer of year. False for current year.
$monthint|falserequired
Integer of month. False for current month.

Return

string The permalink for the specified month and year archive.

More Information

In a Plugin or Theme, it can be used as early as the setup_theme Action. Any earlier usage, including plugins_loaded, generates a Fatal Error.

Source

function get_month_link( $year, $month ) {
	global $wp_rewrite;
	if ( ! $year ) {
		$year = current_time( 'Y' );
	}
	if ( ! $month ) {
		$month = current_time( 'm' );
	}
	$monthlink = $wp_rewrite->get_month_permastruct();
	if ( ! empty( $monthlink ) ) {
		$monthlink = str_replace( '%year%', $year, $monthlink );
		$monthlink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $monthlink );
		$monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
	} else {
		$monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
	}

	/**
	 * Filters the month archive permalink.
	 *
	 * @since 1.5.0
	 *
	 * @param string $monthlink Permalink for the month archive.
	 * @param int    $year      Year for the archive.
	 * @param int    $month     The month for the archive.
	 */
	return apply_filters( 'month_link', $monthlink, $year, $month );
}

Hooks

apply_filters( ‘month_link’, string $monthlink, int $year, int $month )

Filters the month archive permalink.

Changelog

Version Description
1.0.0 Introduced.

User Contributed Notes

  1. Skip to note 6 content

    Use With PHP Variables

    PHP code block for use within The Loop: Assigns year and month of a post to the variables $arc_year and $arc_month. These are used with the get_month_link() tag, which returns the URL as a link to the monthly 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 get_month_link( $archive_year, $archive_month ); ?>">
    Archive for 
    </a>