get_trackback_url()
云策文档标注
概述
get_trackback_url() 函数用于获取当前文章的 trackback URL。它会根据是否启用了固定链接来生成不同的 URL 格式,并返回经过过滤的字符串。
关键要点
- 函数返回当前文章的 trackback URL,类型为字符串。
- 如果启用了固定链接(permalink_structure),则基于文章永久链接生成美观路径的 URL。
- 如果未启用固定链接,则使用文章 ID 拼接基础 URL 生成 trackback URL。
- 返回的 URL 会通过 trackback_url 过滤器进行过滤,允许开发者自定义。
代码示例
function get_trackback_url() {
if ( get_option( 'permalink_structure' ) ) {
$trackback_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' );
} else {
$trackback_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID();
}
return apply_filters( 'trackback_url', $trackback_url );
}注意事项
- 函数依赖于 get_option('permalink_structure') 来判断固定链接状态。
- 使用 trailingslashit() 和 user_trailingslashit() 确保 URL 路径格式正确。
- 可通过 trackback_url 过滤器钩子修改返回的 URL。
原文内容
Retrieves the current post’s trackback URL.
Description
There is a check to see if permalink’s have been enabled and if so, will retrieve the pretty path. If permalinks weren’t enabled, the ID of the current post is used and appended to the correct page to go to.
Source
function get_trackback_url() {
if ( get_option( 'permalink_structure' ) ) {
$trackback_url = trailingslashit( get_permalink() ) . user_trailingslashit( 'trackback', 'single_trackback' );
} else {
$trackback_url = get_option( 'siteurl' ) . '/wp-trackback.php?p=' . get_the_ID();
}
/**
* Filters the returned trackback URL.
*
* @since 2.2.0
*
* @param string $trackback_url The trackback URL.
*/
return apply_filters( 'trackback_url', $trackback_url );
}
Hooks
- apply_filters( ‘trackback_url’, string $trackback_url )
-
Filters the returned trackback URL.
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |