get_year_link()
Gets the URL (link) to the archive of posts for the specified year.
1 time — 0.000159 sec (fast) | 50000 times — 3.51 sec (fast) | PHP 7.0.8, WP 4.6
Hooks from the function
Returns
String. URL
Usage
get_year_link( $year );
- $year(int/bool) (required)
- The year for which to get the archive link. Specify false if you want to get the link for the current year.
Examples
#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
#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> #3 Display Link for the current year’s archive
<a href="<?= get_year_link('') ?>">Posts from this year</a>
Notes
- Global. WP_Rewrite.
$wp_rewriteWordPress rewrite component.
Changelog
| Since 1.5.0 | Introduced. |