permalink_anchor()
云策文档标注
概述
permalink_anchor() 函数用于显示当前文章的固定链接锚点,支持基于标题或ID生成HTML 'id' 属性。
关键要点
- 函数生成一个HTML锚点链接,用于文章内部导航。
- 接受可选参数 $mode,默认为 'id',可选 'title' 或 'id'。
- 在 'title' 模式下,使用文章标题的 sanitize_title() 版本和ID组合作为 'id' 属性。
- 在 'id' 模式下,使用 'post-' 加文章ID作为 'id' 属性。
代码示例
permalink_anchor( $type );注意事项
- 函数直接输出HTML,无返回值。
- 依赖 get_post() 获取当前文章数据。
- 使用 sanitize_title() 确保标题在URL中安全。
原文内容
Displays the permalink anchor for the current post.
Description
The permalink mode title will use the post title for the ‘a’ element ‘id’ attribute. The id mode uses ‘post-‘ with the post ID for the ‘id’ attribute.
Parameters
$modestringoptional-
Permalink mode. Accepts
'title'or'id'. Default'id'.
Source
function permalink_anchor( $mode = 'id' ) {
$post = get_post();
switch ( strtolower( $mode ) ) {
case 'title':
$title = sanitize_title( $post->post_title ) . '-' . $post->ID;
echo '<a id="' . $title . '"></a>';
break;
case 'id':
default:
echo '<a id="post-' . $post->ID . '"></a>';
break;
}
}
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
Skip to note 2 content
Codex
Basic Example
Inserts the permalink anchor next to a post’s title.
<h3></h3>