質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
RSS

RSS(Really Simple Syndication)はブログのエントリやニュースの見出し、標準のフォーマットの音声やビデオなどを発行するために使われるウェブフィードのフォーマットの集合体です。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

1回答

1277閲覧

WordPressの固定ページにRSSフィードを作成したい

mark077

総合スコア10

RSS

RSS(Really Simple Syndication)はブログのエントリやニュースの見出し、標準のフォーマットの音声やビデオなどを発行するために使われるウェブフィードのフォーマットの集合体です。

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2022/03/04 03:07

フィードを出力する所から失敗しており、
初歩的な質問で大変恐縮ですが、ご教授お願い致します。

前提・実現したいこと

WordPressの固定ページにRSSフィードを作成したい
今後別のフォーマットでのフィード作成する可能性も考慮しております。(元のfeedは残したいです)

発生している問題・エラーメッセージ

そもそものやり方が間違っていたら恐縮なのですが、
記事が出力されません。。

自分で調べたことや試したこと

  1. テーマ内にpage-smartnews.phpを作成
  2. 新規固定ページにて/smartnews/を作成
  3. 1で作成したテンプレートを固定ページ/smartnews/に指定
  4. page-smartnews.phpの中身をwp-includes/feed-rss2.phpから流用

functions.phpでフックしてしまうと元のfeedが書き変わってしまうのでは?と難航しております。

デフォルトのURL http://xxx.com/feed/
固定ページURL http://xxx.com/feed/smartnews/

該当のソースコード

html

1<?php 2/* 3* Template Name: feed-smartnews 4* Template Post Type: page 5*/ 6?> 7<?php 8/** 9 * RSS2 Feed Template for displaying RSS2 Posts feed. 10 * 11 * @package WordPress 12 */ 13 14header('Content-Type: ' . feed_content_type('rss2') . '; charset=' . get_option('blog_charset'), true); 15$more = 1; 16 17echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; 18 19/** 20 * Fires between the xml and rss tags in a feed. 21 * 22 * @since 4.0.0 23 * 24 * @param string $context Type of feed. Possible values include 'rss2', 'rss2-comments', 25 * 'rdf', 'atom', and 'atom-comments'. 26 */ 27do_action( 'rss_tag_pre', 'rss2' ); 28?> 29<rss version="2.0" 30 xmlns:content="http://purl.org/rss/1.0/modules/content/" 31 xmlns:wfw="http://wellformedweb.org/CommentAPI/" 32 xmlns:dc="http://purl.org/dc/elements/1.1/" 33 xmlns:atom="http://www.w3.org/2005/Atom" 34 xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 35 xmlns:slash="http://purl.org/rss/1.0/modules/slash/" 36 <?php 37 /** 38 * Fires at the end of the RSS root to add namespaces. 39 * 40 * @since 2.0.0 41 */ 42 do_action( 'rss2_ns' ); 43 ?> 44> 45 46<channel> 47 <title><?php wp_title_rss(); ?></title> 48 <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" /> 49 <link><?php bloginfo_rss('url') ?></link> 50 <description><?php bloginfo_rss("description") ?></description> 51 <lastBuildDate><?php 52 $date = get_lastpostmodified( 'GMT' ); 53 echo $date ? mysql2date( 'D, d M Y H:i:s +0000', $date, false ) : date( 'D, d M Y H:i:s +0000' ); 54 ?></lastBuildDate> 55 <language><?php bloginfo_rss( 'language' ); ?></language> 56 <sy:updatePeriod><?php 57 $duration = 'hourly'; 58 59 /** 60 * Filters how often to update the RSS feed. 61 * 62 * @since 2.1.0 63 * 64 * @param string $duration The update period. Accepts 'hourly', 'daily', 'weekly', 'monthly', 65 * 'yearly'. Default 'hourly'. 66 */ 67 echo apply_filters( 'rss_update_period', $duration ); 68 ?></sy:updatePeriod> 69 <sy:updateFrequency><?php 70 $frequency = '1'; 71 72 /** 73 * Filters the RSS update frequency. 74 * 75 * @since 2.1.0 76 * 77 * @param string $frequency An integer passed as a string representing the frequency 78 * of RSS updates within the update period. Default '1'. 79 */ 80 echo apply_filters( 'rss_update_frequency', $frequency ); 81 ?></sy:updateFrequency> 82 <?php 83 /** 84 * Fires at the end of the RSS2 Feed Header. 85 * 86 * @since 2.0.0 87 */ 88 do_action( 'rss2_head'); 89 90 while( have_posts()) : the_post(); 91 ?> 92 <item> 93 <title><?php the_title_rss() ?></title> 94 <link><?php the_permalink_rss() ?></link> 95<?php if ( get_comments_number() || comments_open() ) : ?> 96 <comments><?php comments_link_feed(); ?></comments> 97<?php endif; ?> 98 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate> 99 <dc:creator><![CDATA[<?php the_author() ?>]]></dc:creator> 100 <?php the_category_rss('rss2') ?> 101 102 <guid isPermaLink="false"><?php the_guid(); ?></guid> 103<?php if (get_option('rss_use_excerpt')) : ?> 104 <description><![CDATA[<?php the_excerpt_rss(); ?>]]></description> 105<?php else : ?> 106 <description><![CDATA[<?php the_excerpt_rss(); ?>]]></description> 107 <?php $content = get_the_content_feed('rss2'); ?> 108 <?php if ( strlen( $content ) > 0 ) : ?> 109 <content:encoded><![CDATA[<?php echo $content; ?>]]></content:encoded> 110 <?php else : ?> 111 <content:encoded><![CDATA[<?php the_excerpt_rss(); ?>]]></content:encoded> 112 <?php endif; ?> 113<?php endif; ?> 114<?php if ( get_comments_number() || comments_open() ) : ?> 115 <wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss> 116 <slash:comments><?php echo get_comments_number(); ?></slash:comments> 117<?php endif; ?> 118<?php rss_enclosure(); ?> 119 <?php 120 /** 121 * Fires at the end of each RSS2 feed item. 122 * 123 * @since 2.0.0 124 */ 125 do_action( 'rss2_item' ); 126 ?> 127 </item> 128 <?php endwhile; ?> 129</channel> 130</rss>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tabuu

2022/03/04 07:50

仮に固定ページでfeedを出力したとして、その固定ページ1件がfeedに出力されるだけです。 何をされたいのでしょうか?
guest

回答1

0

自己解決

自己解決済

①テーマ内のfunctions.phpに以下を追記

functions.php

1// SmartNews のフィードを追加 2add_action('init', function (){ 3 add_feed('smartnews', function () { 4 get_template_part('temp','smartnews'); 5 }); 6}); 7// SmartNews のフィードの Content-type をカスタマイズ 8add_filter( 'feed_content_type', 'custom_feed_content_type', 10, 2); 9function custom_feed_content_type($content_type, $type){ 10 if('smartnews' === $type ){ 11 return feed_content_type('rss2'); 12 } 13 return $content_type; 14}

② temp-smartnews.phpを新規作成
③/smartnews/で作成していた固定ページは削除

投稿2022/03/16 00:13

mark077

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問