WordPress函数:get_archives_link 获取归档链接

江河/ 2023年07月10日/ WordPress/ 浏览 917

函数原型:


get_archives_link( string $url, string $text, string $format = 'html', string $before = '', string $after = '', bool $selected = false ): string


格式可以是四种样式之一。head元素的'link',select元素中使用的'option',list中使用的html(ol或ul html元素)。使用before和after参数也支持自定义内容。


“链接”格式使用具有存档关系的<link>HTML元素。不使用before和after参数。text参数用于描述链接。


“option”格式在select元素中使用选项HTML元素。


该值是url参数,在文本描述之间使用before和after参数。


“html”格式是默认格式,它使用li html元素来在列表html元素中使用。before参数在链接之前,after参数在关闭链接之后。


自定义格式在链接('a'HTML元素)之前使用before参数,在关闭链接标记之后使用after参数。如果没有使用上述三个格式值,则假定使用自定义格式


参数说明:


$url 归档链接。


$text 归档文字描述。


$format 'link','option','html',默认:html。


$before 链接前方文字。


$after 链接后方文字。


$selected 如果当前页面是选定的存档页面,则设置为true。


函数源码:


function get_archives_link( $url, $text, $format = 'html', $before = '', $after = '', $selected = false ) {
	$text         = wptexturize( $text );
	$url          = esc_url( $url );
	$aria_current = $selected ? ' aria-current="page"' : '';

	if ( 'link' === $format ) {
		$link_html = "\t<link rel='archives' title='" . esc_attr( $text ) . "' href='$url' />\n";
	} elseif ( 'option' === $format ) {
		$selected_attr = $selected ? " selected='selected'" : '';
		$link_html     = "\t<option value='$url'$selected_attr>$before $text $after</option>\n";
	} elseif ( 'html' === $format ) {
		$link_html = "\t<li>$before<a href='$url'$aria_current>$text</a>$after</li>\n";
	} else { // Custom.
		$link_html = "\t$before<a href='$url'$aria_current>$text</a>$after\n";
	}

	return apply_filters( 'get_archives_link', $link_html, $url, $text, $format, $before, $after, $selected );
}


包含钩子:


apply_filters( 'get_archives_link', string $link_html, string $url, string $text, string $format, string $before, string $after, bool $selected )


使用举例:


function wpdocs_archive_count_span( $links ) {
 	$links = str_replace( '</a>&nbsp;(', '<span class="count">', $links );
	$links = str_replace( ')', '</span></a>', $links );
	return $links;
}
add_filter( 'get_archives_link', 'wpdocs_archive_count_span' );


发表评论

暂无评论,抢个沙发...

客服 工单