get_year_link()WP 1.5.0

Retrieves the permalink for the year archives.

1 time — 0.000159 sec (fast) | 50000 times — 3.51 sec (fast) | PHP 7.0.8, WP 4.6
Hooks from the function

Return

String. The permalink for the specified year archive.

Usage

get_year_link( $year );
$year(int|false) (required)
Integer of year. False for current year.

Examples

0

#1 Get the link to the archive of the year

echo get_year_link( false ); //> http://example.com/2022 - current year

echo get_year_link( 2014 ); //> http://example.com/2014
0

#2 Using With PHP Variables

PHP code block for use within The Loop: Assigns year to the variable. This is used with the get_year_link() tag, which returns the URL as a link to the yearly archive for a 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="<?= get_year_link( get_the_time('Y') ); ?>">
	<?php the_time('Y'); ?> archive
</a>
0

#3 Display Link for the current year’s archive

<a href="<?= get_year_link('') ?>">Posts from this year</a>

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 1.5.0 Introduced.

get_year_link() code WP 6.4.3

function get_year_link( $year ) {
	global $wp_rewrite;
	if ( ! $year ) {
		$year = current_time( 'Y' );
	}
	$yearlink = $wp_rewrite->get_year_permastruct();
	if ( ! empty( $yearlink ) ) {
		$yearlink = str_replace( '%year%', $year, $yearlink );
		$yearlink = home_url( user_trailingslashit( $yearlink, 'year' ) );
	} else {
		$yearlink = home_url( '?m=' . $year );
	}

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