How to get the link to the yearly archive in WordPress

The get_year_link function in WordPress is designed to retrieve the URL for the yearly archives. This function is part of WordPress’s template tag system and is primarily used when creating navigation links to yearly archives in a WordPress site.

When called, the get_year_link function generates a URL that points to the archive page for a specific year. This archive page displays all the posts that were published in that year. The function takes a year as an argument and returns a string representing the URL for the archive page of that year.

In terms of its application, the get_year_link function can be useful in various scenarios. For instance, if you are building a blog site and want to provide your users with an option to view all posts from a specific year, this function can be used to generate the necessary URLs. Similarly, it can be used in creating a custom navigation menu where users can browse posts by year.

It’s important to note that the get_year_link function only generates the URL for the archive page. It does not actually output the link. To display the link on a page, you would need to use the function in conjunction with the echo command or within a hyperlink HTML tag.

Parameters of the get_year_link Function

The get_year_link function in WordPress accepts a single parameter:

  • $year(int|false): This is a mandatory parameter. It should be an integer representing the year. If you want to use the current year, you should pass ‘false’.

Return Value of the get_year_link Function

The get_year_link function returns a string value. This string is the permalink for the archive of the year specified in the parameter. If ‘false’ was passed as the parameter, the function will return the permalink for the archive of the current year.

If the function doesn’t accept any parameters, it will be clearly stated in the function’s description.

Examples

How to Display a Link to the Current Year Archive

<?php 
$current_year = date('Y');
echo '<p>Archive for year: <a href="'. get_year_link($current_year) .'">' . $current_year . '</a></p>';
?>

This code snippet will display a link to the current year’s archive. It first gets the current year using the date function and stores it in the $current_year variable. Then it uses the get_year_link function to generate the URL for that year’s archive. The URL is then placed inside an anchor tag to create a clickable link.

How to Display a Link to a Specific Year Archive

<?php 
$year = 2020;
echo '<p>Archive for year: <a href="' . get_year_link($year) . '">' . $year . '</a></p>';
?>

This code snippet will display a link to the archive for the year 2020. The year is stored in the $year variable and then passed to the get_year_link function to generate the URL for that year’s archive. The URL is then placed inside an anchor tag to create a clickable link.

How to Display Links to a Range of Year Archives

<?php 
$start_year = 2010;
$end_year = 2020;
for ($year = $start_year; $year <= $end_year; $year++) {
 echo '<p>Archive for year: <a href="' . get_year_link($year) . '">' . $year . '</a></p>';
}
?>

This code snippet will display links to the archives for the years 2010 through 2020. It uses a for loop to iterate over each year in the range, and for each year it generates a URL using the get_year_link function and displays it as a clickable link.

Conclusion

The get_year_link function in WordPress is a built-in function that helps in retrieving the URL for the yearly archive. This function is commonly used in themes and plugins to generate links to the archive pages for specific years. It is particularly useful when you want to provide a chronological navigation system on your website, allowing visitors to browse content from different years. Remember, the output of the get_year_link function is a complete URL, and it can be directly used in an href attribute of an anchor tag.

Related WordPress Functions