get_year_link()
Retrieves the permalink for the year archives.
1 time — 0.000159 sec (fast) | 50000 times — 3.51 sec (fast) | PHP 7.0.8, WP 4.6
Hooks from the function
Return
String
. The permalink for the specified year archive.
Usage
get_year_link( $year );
- $year(int|false) (required)
- Integer of year. False for 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_rewrite WordPress rewrite component.
Changelog
Since 1.5.0 | Introduced. |