get_year_link( int|false $year )

Retrieves the permalink for the year archives.


Parameters Parameters

$year

(int|false) (Required) Integer of year. False for current year.


Top ↑

Return Return

(string) The permalink for the specified year archive.



Top ↑

Source Source

File: wp-includes/link-template.php

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 );
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 3 content
    Contributed by Codex

    Using With PHP Variables

    PHP code block for use within The Loop: Assigns year to the variable $arc_year. 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.

    <?php $archive_year = get_the_time('Y'); ?>
    
    <a href="<?php echo get_year_link( $archive_year ); ?>"><?php the_time('Y'); ?> archive</a>
    

You must log in before being able to contribute a note or feedback.