Using WordPress ‘get_year_link()’ PHP function

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

Displays the link for the current year’s archive:

<a href="<?php echo get_year_link(false); ?>">Current Year Posts</a>

Displays the link for the 2015 archive:

<a href="<?php echo get_year_link(2015); ?>">2015 Posts</a>

Saves the URL for the 2018 archive to a variable $year2018:

$year2018 = get_year_link(2018);

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>

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