add_feed( string $feedname, callable $function )
添加新的 Feed 地址,例如想这样的 /atom1/
。
参数
- $feedname
- (string)
(Required)
Feed name. - $function
- (callable)
(Required)
Callback to run on feed display.
返回值
(string) Feed 动作的名称。
源代码
File: wp-includes/rewrite.php
function add_feed( $feedname, $function ) {
global $wp_rewrite;
if ( ! in_array( $feedname, $wp_rewrite->feeds ) ) {
$wp_rewrite->feeds[] = $feedname;
}
$hook = 'do_feed_' . $feedname;
// Remove default function hook
remove_action( $hook, $hook );
add_action( $hook, $function, 10, 2 );
return $hook;
}
更新日志
Version | 描述 |
---|---|
2.1.0 | Introduced. |
相关函数
Uses
- wp-includes/plugin.php:remove_action()
- wp-includes/plugin.php:add_action()
User Contributed Notes
当添加一个新的 Feed,用户浏览器会以 Content-Type: application/octet-stream; charset=UTF-8
来解析文档。
例子
function add_custom_feed() {
add_feed( 'custom', 'render_custom_feed' );
}
add_action( 'init', 'add_custom_feed' );
function render_custom_feed() {
header( 'Content-Type: application/rss+xml' );
echo 'aye!';
}
或者下面的示例
function add_custom_feed() {
add_feed( 'custom', 'render_custom_feed' );
}
add_action( 'init', 'add_custom_feed' );
function custom_feed_content_type( $content_type, $type ) {
if( 'custom' == $type ) {
$content_type = 'application/rss+xml';
}
return $content_type;
}
add_filter( 'feed_content_type', 'custom_feed_content_type', 10, 2 );
function render_custom_feed() {
echo 'aye!';
}
上面两个示例都能很好的工作。
你可能对这些文章感兴趣:
- wordpress函数edit_user()用法示例
- wordpress函数email_exists()用法示例
- wordpress函数endElement()用法示例
- wordpress函数edit_term_link()用法示例
- wordpress函数edit_tag_link()用法示例
- wordpress函数edit_post_link()用法示例
- wordpress函数edit_link()用法示例
- wordpress函数edit_post()用法示例
- wordpress函数edit_comment_link()用法示例
- wordpress函数edit_form_image_editor()用法示例
- wordpress函数edit_bookmark_link()用法示例
- wordpress函数edit_comment()用法示例
- wordpress函数drop_index()用法示例
- wordpress函数dynamic_sidebar()用法示例
如有疑问,请前往问答中心反馈!
反馈