get_archives_link()
Gets the link to the archive page. Assembles the link from the provided: URL, link anchor, output format, text before and after.
This is a helper function that assembles the HTML link tag in the format specified in the $format parameter:
The text is processed by the filters: wptexturize() and esc_attr(), and the link by esc_url(), and as a result, we will get cleaned text, even if incorrect characters are passed to the function.
Hooks from the function
Returns
String. HTML tag A.
Usage
get_archives_link( $url, $text, $format, $before, $after );
- $url(string) (required)
- URL link:
http://example.com/2013. - $text(string) (required)
- Link anchor (description of the link, text that will be shown as a link).
- $format(string)
In what format to output the link:
html- for use in lists: ul or ol;link- for use in the head part of the HTML code;option- for use in select form elements;custom- another format, leave this field empty and use the before and after parameters.
Default: html
- $before(string)
- Text before the link, HTML code can be specified.
Default: '' - $after(string)
- Text after the link, HTML code can be specified.
Default: ''
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. |