Feedをカスタマイズ

WordPressが自動で出力してくれるFeedは、全文か抜粋かを選ぶだけのシンプルなものとなっている。当ブログではこれで十分であるが、フォトログだったらやはりFeedに画像なんかを表示したいものだ。いろいろ調べているうちに、Feedの内容はテーマやプラグインでカスタマイズできることが分かった。

以下はテーマでatomの出力内容を変更する例。

remove_action('do_feed_atom', 'do_feed_atom', 10, 1);
automatic_feed_links(false);

function custom_feed_atom( $for_comments ) {
$template_file = '/feed-atom' . ( $for_comments ? '-comments' : '' ) . '.php';
$template_file = ( file_exists( get_template_directory() . $template_file )
? get_template_directory()
: ABSPATH . WPINC
) . $template_file;
load_template( $template_file );
}
add_action('do_feed_atom', 'custom_feed_atom', 10, 1);

まずfunctions.phpで、本来のatom出力である”do_feed_atom”をremove_actionで停止し、独自の関数をadd_actionで再定義する。
次に、/wp-includes/feed-atom.phpを同じファイル名のままテーマフォルダにコピーする。中身を見るとFeedを出力するテンプレートとなっている。Feedの本文はthe_excerpt_rss();前後になるので、このあたりに画像を表示するなり手を加えるとよい。
さらに、同じ処理をrdf、rss、rss2に関しても行う。

コメントを残す

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>