feed_links()
云策文档标注
概述
feed_links() 函数用于在 WordPress 主题中显示通用订阅源链接,包括文章和评论的 RSS 或 Atom 链接。它依赖于主题对 'automatic-feed-links' 的支持,并允许通过参数和过滤器进行自定义。
关键要点
- 函数功能:输出文章和评论的订阅源链接,如 RSS 或 Atom 链接。
- 依赖条件:仅在当前主题支持 'automatic-feed-links' 时执行,否则返回空。
- 参数:可选 $args 数组,可自定义分隔符和标题文本,默认值已定义。
- 过滤器:提供 feed_links_args、feed_links_show_posts_feed 和 feed_links_show_comments_feed 过滤器,用于修改参数和控制链接显示。
- 相关函数:涉及 get_feed_link()、feed_content_type()、get_default_feed() 等,用于生成链接和内容类型。
代码示例
function feed_links( $args = array() ) {
if ( ! current_theme_supports( 'automatic-feed-links' ) ) {
return;
}
$defaults = array(
'separator' => _x( '»', 'feed link' ),
'feedtitle' => __( '%1$s %2$s Feed' ),
'comstitle' => __( '%1$s %2$s Comments Feed' ),
);
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'feed_links_args', $args );
if ( apply_filters( 'feed_links_show_posts_feed', true ) ) {
printf(
'' . "n",
feed_content_type(),
esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
esc_url( get_feed_link() )
);
}
if ( apply_filters( 'feed_links_show_comments_feed', true ) ) {
printf(
'' . "n",
feed_content_type(),
esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
esc_url( get_feed_link( 'comments_' . get_default_feed() ) )
);
}
}注意事项
- 确保主题已添加 add_theme_support('automatic-feed-links') 以启用此功能。
- 使用过滤器可以灵活控制链接的显示和参数,例如通过 add_filter() 修改 feed_links_show_comments_feed 返回 false 来隐藏评论订阅链接。
- 函数输出 HTML 链接标签,需在主题模板适当位置调用,如 header.php。
原文内容
Displays the links to the general feeds.
Parameters
$argsarrayoptional-
Optional arguments.
Default:
array()
Source
function feed_links( $args = array() ) {
if ( ! current_theme_supports( 'automatic-feed-links' ) ) {
return;
}
$defaults = array(
/* translators: Separator between site name and feed type in feed links. */
'separator' => _x( '»', 'feed link' ),
/* translators: 1: Site title, 2: Separator (raquo). */
'feedtitle' => __( '%1$s %2$s Feed' ),
/* translators: 1: Site title, 2: Separator (raquo). */
'comstitle' => __( '%1$s %2$s Comments Feed' ),
);
$args = wp_parse_args( $args, $defaults );
/**
* Filters the feed links arguments.
*
* @since 6.7.0
*
* @param array $args An array of feed links arguments.
*/
$args = apply_filters( 'feed_links_args', $args );
/**
* Filters whether to display the posts feed link.
*
* @since 4.4.0
*
* @param bool $show Whether to display the posts feed link. Default true.
*/
if ( apply_filters( 'feed_links_show_posts_feed', true ) ) {
printf(
'<link rel="alternate" type="%s" title="%s" href="%s" />' . "n",
feed_content_type(),
esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
esc_url( get_feed_link() )
);
}
/**
* Filters whether to display the comments feed link.
*
* @since 4.4.0
*
* @param bool $show Whether to display the comments feed link. Default true.
*/
if ( apply_filters( 'feed_links_show_comments_feed', true ) ) {
printf(
'<link rel="alternate" type="%s" title="%s" href="%s" />' . "n",
feed_content_type(),
esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
esc_url( get_feed_link( 'comments_' . get_default_feed() ) )
);
}
}
Hooks
- apply_filters( ‘feed_links_args’, array $args )
-
Filters the feed links arguments.
- apply_filters( ‘feed_links_show_comments_feed’, bool $show )
-
Filters whether to display the comments feed link.
- apply_filters( ‘feed_links_show_posts_feed’, bool $show )
-
Filters whether to display the posts feed link.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |