The get_year_link() WordPress PHP function retrieves the permalink for the year archives.
Usage
get_year_link($year);
Example:
Input:
echo get_year_link(2021);
Output:
https://example.com/2021/
Parameters
$year int|false
: Integer of year. False for current year.
More information
See WordPress Developer Resources: get_year_link
Examples
Display Current Year Archive Link
Displays the link for the current year’s archive:
<a href="<?php echo get_year_link(false); ?>">Current Year Posts</a>
Display Archive Link for a Specific Year
Displays the link for the 2015 archive:
<a href="<?php echo get_year_link(2015); ?>">2015 Posts</a>
Save Year Archive Link to a Variable
Saves the URL for the 2018 archive to a variable $year2018
:
$year2018 = get_year_link(2018);
Display Archive Link for the Post’s Year
Displays the link for the archive of the year when the post was published (must be used within The Loop):
$archive_year = get_the_time('Y'); <a href="<?php echo get_year_link($archive_year); ?>"><?php the_time('Y'); ?> Archive</a>
Display Archive Links for a Range of Years
Displays archive links for each year from 2010 to 2020:
for ($year = 2010; $year <= 2020; $year++) { echo '<a href="' . get_year_link($year) . '">' . $year . ' Posts</a><br>'; }