get_archives_link()
Retrieve archive link content based on predefined or custom code.
The format can be one of four styles. The 'link' for head element, 'option' for use in the select element, 'html' for use in list (either ol or ul HTML elements). Custom content is also supported using the before and after parameters.
The 'link' format uses the <link> HTML element with the archives relationship. The before and after parameters are not used. The text parameter is used to describe the link.
The 'option' format uses the option HTML element for use in select element. The value is the url parameter and the before and after parameters are used between the text description.
The 'html' format, which is the default, uses the li HTML element for use in the list HTML elements. The before parameter is before the link and the after parameter is after the closing link.
The custom format uses the before parameter before the link ('a' HTML element) and the after parameter after the closing link tag. If the above three values for the format are not used, then custom format is assumed.
Hooks from the function
Return
String
. HTML link content for archive.
Usage
get_archives_link( $url, $text, $format, $before, $after, $selected );
- $url(string) (required)
- URL to archive.
- $text(string) (required)
- Archive text description.
- $format(string)
- Can be 'link', 'option', 'html', or custom.
Default: 'html' - $before(string)
- Content to prepend to the description.
Default: '' - $after(string)
- Content to append to the description.
Default: '' - $selected(true|false)
- Set to true if the current page is the selected archive page.
Default: false
Examples
#1 Examples showing what the function returns:
echo get_archives_link( '/2013', '2013 Archive' ); // <li><a href='/2013' title='Archive for 2013'>Archive for 2013</a></li> get_archives_link( '/2013', '2013 Archive', 'link' ); // <link rel='archives' title='Archive for 2013' href='/2013' /> get_archives_link( '/2013', '2013 Archive', 'option' ); // <option value='/2013'> Archive for 2013 </option> get_archives_link( '/2013', '2013 Archive', '', '<div>','</div>' ); // <div><a href='/2013' title='Archive for 2013'>Archive for 2013</a></div>
#2 Wrap the number of posts in the archive widget in HTML
Let's add a <span> around the number of posts in the archive widget. This allows you to easily style the number of posts.
add_filter( 'get_archives_link', 'wpdocs_archive_count_span' ); /** * Adds a span around post counts in the archive widget. * * @param string $links The comment fields. * @return string */ function wpdocs_archive_count_span( $links ) { $links = str_replace( '</a> (', '<span class="count">', $links ); $links = str_replace( ')', '</span></a>', $links ); return $links; }
Changelog
Since 1.0.0 | Introduced. |
Since 5.2.0 | Added the $selected parameter. |