概要
the_excerpt_rss
フィルタは、RSSフィード用の抜粋記事を出力する際に使用されます。このフィルタを利用することで、RSSフィードにおける投稿の抜粋のカスタマイズや、特定の形式での出力が可能になります。主な使用ケースとしては、以下のようなものがあります。
- 投稿の内容から特定の部分だけを抜粋表示
- 特定のカスタムフィールドをフィードに追加
- 抜粋のフォーマッティングを変更
- マルチバイト文字の処理を行う
- 投稿の抜粋にリンクを追加
- 特定のカテゴリーやタグに基づいて抜粋を調整
- 短縮URLを生成して表示
- タイムスタンプや日付をフォーマットして表示
構文
the_excerpt_rss
フィルタは、以下のような構文で使用されます。
add_filter('the_excerpt_rss', 'your_custom_function');
パラメータ
- $post_excerpt: フィルタリングされる抜粋のテキスト。
戻り値
- フィルタを適用した後の抜粋のテキスト。
関連する関数
使用可能なバージョン
the_excerpt_rss
フィルタは、WordPress 2.0以降で使用可能です。
コアファイルのパス
the_excerpt_rss
フィルタは、以下のファイルに含まれています。
/wp-includes/feed.php
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプルコード1: RSSフィードの抜粋にカスタムフィールドを追加
add_filter('the_excerpt_rss', function($post_excerpt) {
$custom_field = get_post_meta(get_the_ID(), 'custom_field_key', true);
if ($custom_field) {
$post_excerpt .= '<p>Custom Field: ' . esc_html($custom_field) . '</p>';
}
return $post_excerpt;
});
このコードは、投稿のRSSフィードの抜粋にカスタムフィールドの値を追加します。
サンプルコード2: 抜粋のテキストを加工する
add_filter('the_excerpt_rss', function($post_excerpt) {
return ucfirst($post_excerpt);
});
このコードは、抜粋の最初の文字を大文字にすることで、テキストを加工します。
サンプルコード3: 特定の短縮URLを追加する
add_filter('the_excerpt_rss', function($post_excerpt) {
return $post_excerpt . '<p>Read more: <a href="https://example.com">Example Site</a></p>';
});
このコードは、抜粋の最後に「Read more」のリンクを追加します。
サンプルコード4: 文字列をHTMLエンティティに変換
add_filter('the_excerpt_rss', function($post_excerpt) {
return htmlspecialchars($post_excerpt);
});
このコードは、抜粋内のテキストをHTMLエンティティに変換し、表示を安全にします。
サンプルコード5: 抜粋テキストの文字数制限
add_filter('the_excerpt_rss', function($post_excerpt) {
return wp_trim_words($post_excerpt, 30, '...');
});
このコードは、抜粋を30単語に制限し、30単語を超えた場合は「…」を追加します。