函数文档

_post_format_link()

💡 云策文档标注

概述

_post_format_link() 是一个 WordPress 过滤器函数,用于从文章格式术语链接中移除格式前缀。它根据重写规则状态处理链接,确保链接格式正确。

关键要点

  • 函数作用:过滤文章格式术语链接,移除 'post-format-' 前缀。
  • 参数:接受 $link(链接字符串)、$term(WP_Term 对象)、$taxonomy(分类法字符串)三个参数。
  • 条件检查:仅当 $taxonomy 为 'post_format' 时执行处理,否则直接返回原链接。
  • 处理逻辑:如果存在额外固定链接结构,则替换 slug 中的前缀;否则通过查询参数调整链接。
  • 返回值:返回处理后的链接字符串。

代码示例

function _post_format_link( $link, $term, $taxonomy ) {
    global $wp_rewrite;
    if ( 'post_format' !== $taxonomy ) {
        return $link;
    }
    if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
        return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
    } else {
        $link = remove_query_arg( 'post_format', $link );
        return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
    }
}

注意事项

  • 函数自 WordPress 3.1.0 版本引入。
  • 依赖全局变量 $wp_rewrite 和函数如 remove_query_arg()、add_query_arg()。
  • 主要用于内部处理,开发者通常无需直接调用,但可了解其机制以自定义链接行为。

📄 原文内容

Filters the post format term link to remove the format prefix.

Parameters

$linkstringrequired
$termWP_Termrequired
$taxonomystringrequired

Return

string

Source

function _post_format_link( $link, $term, $taxonomy ) {
	global $wp_rewrite;
	if ( 'post_format' !== $taxonomy ) {
		return $link;
	}
	if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {
		return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );
	} else {
		$link = remove_query_arg( 'post_format', $link );
		return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );
	}
}

Changelog

Version Description
3.1.0 Introduced.