Retrieves the permalink for the year archives.
Parameters
$year
int|falserequired- Integer of year. False for current year.
Source
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 );
}
Hooks
- apply_filters( ‘year_link’,
string $yearlink ,int $year ) Filters the year archive permalink.
Changelog
Version | Description |
---|---|
1.5.0 | Introduced. |
Year as Link
Returns the URL for the current year’s archive, displaying it as a link in the anchor tag by using the PHP echo command.
Year as a variable
Returns URL for the archive year 2003, assigning it to the variable $year03. The variable can then be used elsewhere in a page.
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.