apply_filters( 'wpml_element_link', int $element_id, string $element_type, string $link_text, array $optional_parameters, string $anchor, bool $echo, bool $return_original_if_missing )
- 类型
- filter
- 类别
- 获取本地化内容
- WPML 版本
- 3.2
说明
返回当前语言的文章类型链接或分类法项链接。
* 注意:还有其他方法可以获取元素的链接,这能让开发人员对 HTML 输出拥有更全面的控制。
WordPress 提供了许多函数来实现这一点,例如 get_permalink()、get_term_link() 等。
如果您打算使用的函数需要将文章类型或分类项 ID 作为参数,那么您可能需要查看 wpml_object_id 过滤器。
参数
- $element_id
- (int) (必填) 要链接到的文章类型(post、page、attachment、custom post)或分类法术语(tag、category、custom taxonomy)的 ID
- $element_type
- (string) (可选) 要链接到的元素类型。可以是 ‘post’、'page’、'attachment’、'{custom post}'、'tag’、'category’、'{custom taxonomy}'。默认为 ‘post’
- $link_text
- (string) (可选) 链接文本。默认为元素的名称
- $optional_parameters
- (array) (可选) 链接的参数
- $anchor
- (string) (可选) 链接的锚点
- $echo
- (bool) (可选) 0|false 表示返回,1|true 表示输出本地化链接。默认为 true
- $return_original_if_missing
- (bool) (可选) 如果设置为 true,则始终返回一个值(如果缺少翻译,则返回原始值)。默认为 TRUE
使用示例
示例
//produces: <a href="/hello-world/">Hello World!</a> apply_filters( 'wpml_element_link', 1 ); //produces: <a href="/sample-page/">Custom link text for the sample page</a> apply_filters( 'wpml_element_link', 2, 'page', __( 'Custom link text for the sample page' ) ); //produces: <a href="/sample-page/?category=foo&bar=baz">Sample Page</a> apply_filters( 'wpml_element_link', 2, 'page', '', array( 'category'=>'foo', 'bar'=>'baz' ) ); //produces: <a href="/sample-page/#contact">Sample Page</a> apply_filters( 'wpml_element_link', 2, 'page', '', '', 'contact' ); //produces: <a href="/category/uncategorized/">Uncategorized</a> apply_filters( 'wpml_element_link', 1, 'category' ); //produces: <a href="/zh-hans/?attachment_id=25">'Attachment image title</a> apply_filters( 'wpml_element_link', 25, 'attachment', __( 'Attachment image title' ) );
相关钩子
wpml_media_url, wpml_media_id, force_suppress_filters, wpml_get_translated_slug, wpml_home_url, wpml_translate_single_string, wpml_translate_string, wpml_elements_without_translations, wpml_permalink, wpml_unfiltered_admin_string