
WordPressでは、フィードの設定を行うことでSmartNews形式に対応させることが可能です。
以下では、簡単にできるようにコピペと、ちょっとした入力でカスタマイズできる方法を紹介します。
目次
主な手順

WordPressのフィードをSmartNews対応させるのに必要な主な手順はこちら。
- 子テーマ直下にsmartnews.phpテンプレートの作成
- SmartNewsフィード用のコードを貼り付ける
- コード内に変数値を入力する
- functions.phpでSmartNewsフィードを呼び出す
基本的に、コピペ2回と、ちょっとした値入力で実装できるかと思います。
子テーマ直下にsmartnews.phpテンプレートの作成
まずは、子テーマフォルダ直下に「smartnews.php」というテンプレートファイルを作成します。
SmartNewsフィード用のコードを貼り付ける
テンプレートファイルを開いて以下のコードを貼り付ける。
<?php
/**
* RSS2 Feed Template for displaying RSS2 Posts feed.
*
* @package SmartNews
*/
/**
* 寝ログ
* @author: yhira
* @link: https://nelog.jp/smaetnews-feed
* @license: http://www.gnu.org/licenses/gpl-2.0.html GPL v2 or later
*/
if ( !defined( 'ABSPATH' ) ) exit;
$logo_url = 'サイトロゴのURL';
$no_image_url = 'アイキャッチがない場合の画像URL';
$tracking_id = 'Google AnalyticsのトラッキングID';
$site_domain = 'サイトドメイン';
// //入力例
// $logo_url = 'https://im-cocoon.net/wp-content/uploads/nelog-logo.png';
// $no_image_url = 'https://im-cocoon.net/wp-content/uploads/no-image.jpeg';
// $tracking_id = 'UA-775245XXX';
// $site_domain = 'nelog.jp';
header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . get_option( 'blog_charset' ), true );
$more = 1;
echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>';
/**
* Fires between the xml and rss tags in a feed.
*
* @since 4.0.0
*
* @param string $context Type of feed. Possible values include 'rss2', 'rss2-comments',
* 'rdf', 'atom', and 'atom-comments'.
*/
do_action( 'rss_tag_pre', 'rss2' );
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:snf="http://www.smartnews.be/snf"
<?php
/**
* Fires at the end of the RSS root to add namespaces.
*
* @since 2.0.0
*/
do_action( 'rss2_ns' );
?>
>
<channel>
<title><?php wp_title_rss(); ?></title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss( 'url' ); ?></link>
<description><?php bloginfo_rss( 'description' ); ?></description>
<lastBuildDate>
<?php
$date = get_lastpostmodified( 'GMT' );
echo $date ? mysql2date( 'r', $date, false ) : date( 'r' );
?>
</lastBuildDate>
<copyright><?php bloginfo_rss('name'); ?> All rights reserved.</copyright>
<snf:logo>
<url><?php echo $logo_url; ?></url>
</snf:logo>
<language><?php bloginfo_rss( 'language' ); ?></language>
<sy:updatePeriod>
<?php
$duration = 'hourly';
/**
* Filters how often to update the RSS feed.
*
* @since 2.1.0
*
* @param string $duration The update period. Accepts 'hourly', 'daily', 'weekly', 'monthly',
* 'yearly'. Default 'hourly'.
*/
echo apply_filters( 'rss_update_period', $duration );
?>
</sy:updatePeriod>
<sy:updateFrequency>
<?php
$frequency = '1';
/**
* Filters the RSS update frequency.
*
* @since 2.1.0
*
* @param string $frequency An integer passed as a string representing the frequency
* of RSS updates within the update period. Default '1'.
*/
echo apply_filters( 'rss_update_frequency', $frequency );
?>
</sy:updateFrequency>
<?php
/**
* Fires at the end of the RSS2 Feed Header.
*
* @since 2.0.0
*/
do_action( 'rss2_head' );
while ( have_posts() ) :
the_post();
?>
<item>
<title><?php the_title_rss(); ?></title>
<link><?php the_permalink_rss(); ?></link>
<?php if ( get_comments_number() || comments_open() ) : ?>
<comments><?php comments_link_feed(); ?></comments>
<?php endif; ?>
<pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
<dc:creator><![CDATA[<?php the_author(); ?>]]></dc:creator>
<?php the_category_rss( 'rss2' ); ?>
<guid isPermaLink="false"><?php the_guid(); ?></guid>
<?php if (get_option('rss_use_excerpt')) : ?>
<description><![CDATA[<?php echo the_excerpt_rss(); ?>]]></description>
<?php endif; ?>
<?php
$content = get_the_content_feed('rss2');
//aリンクは含めない。SmartNewsの仕様?リンクが多くあると以下のエラーが出る
//item.content:encoded の記事内に多くのリンクが含まれています - item.title: 記事のタイトル
//https://publishers.smartnews.com/ja/smartformat/specification_rss/
$content = preg_replace('{<a [^>]+?>}i', '', $content);
$content = str_replace('</a>', '', $content);
?>
<content:encoded><![CDATA[<?php echo $content; ?>]]></content:encoded>
<?php if ( get_comments_number() || comments_open() ) : ?>
<wfw:commentRss><?php echo esc_url( get_post_comments_feed_link( null, 'rss2' ) ); ?></wfw:commentRss>
<slash:comments><?php echo get_comments_number(); ?></slash:comments>
<?php endif; ?>
<?php rss_enclosure(); ?>
<?php
/**
* Fires at the end of each RSS2 feed item.
*
* @since 2.0.0
*/
do_action( 'rss2_item' );
?>
<?php //アイキャッチの取得
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id, true);
if (isset($image_url[0])) {
$thumbnail = $image_url[0];
} else {
$thumbnail = $no_image_url;
}
?>
<media:thumbnail url="<?php echo $thumbnail; ?>" />
<snf:analytics><![CDATA[
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '<?php echo $tracking_id; ?>', '<?php echo $site_domain; ?>');
ga('require', 'displayfeatures');
ga('set', 'referrer', 'http://www.smartnews.com/');
ga('send', 'pageview', '<?php echo str_replace(home_url(), '', get_permalink()); ?>');
</script>
]]>
</snf:analytics>
</item>
<?php endwhile; ?>
</channel>
</rss>
上記コードは、WordPressのインストールディレクトリにある、「wp-includes/feed-rss2.php」をカスタマイズしたものです。
コード内に必要な変数値を入力する
コード内の以下の部分を
$logo_url = 'サイトロゴのURL'; $no_image_url = 'アイキャッチがない場合の画像URL'; $tracking_id = 'Google AnalyticsのトラッキングID'; $site_domain = 'サイトドメイン';
以下のように自分の環境に合ったものに変更します。
//入力例 $logo_url = 'https://im-cocoon.net/wp-content/uploads/nelog-logo.png'; $no_image_url = 'https://im-cocoon.net/wp-content/uploads/no-image.jpeg'; $tracking_id = 'UA-775245XXX'; $site_domain = 'nelog.jp';
functions.phpでSmartNewsフィードを呼び出す
あとは子テーマのfunctions.phpに以下のコードをコピペで追記します。
//SmartNewsフィード追加
add_action('init', function (){
add_feed('smartnews', function () {
get_template_part('smartnews');
});
});
//SmartNewsのHTTP header for Content-type
add_filter( 'feed_content_type', function ( $content_type, $type ) {
if ( 'smartnews' === $type ) {
return feed_content_type( 'rss2' );
}
return $content_type;
}, 10, 2 );
上記コードでSmartNewsフィードをWordPressに登録しています。
動作確認
動作確認を行うには、以下のURLでフィードを出力させ確認します。
http://ドメイン/?feed=smartnews
サンプルフィードはこちら。
Chromeで確認すると、以下のようなコードが出力されているのがわかります。
SmartNewsには、「SmartFormat チェックツール」というフィードチェックツールもあります。
チェックツールに、「フィードのURL」や「XMLファイル」をポストすることでSmartNewsフォーマットに適合しているか手軽にチェックすることができます。
うまく動作していれば「結果」の部分に「Valid」と表示されます。
画面下の方に行けば、ソースコードやプレビュー画面を見ることができます。
ソースHTML。
プレビュー。
まとめ
今回の方法を利用すれば、比較的簡単にSmartNewsフィード対応させることができるかと思います。
WordPressカスタマイズに慣れている方であれば、5分もかからないかと思います。
ただ、フィードを出力できたとしても、SmartNewsは登録制です。
結局のところ、申請に合格しなければ、意味はないかもしれません。
SmartFormatの仕様書はこちら。







はじめまして。
他サイトではうまくいかなかったSmartNews対応フィードがこちらの記事のおかげでできました。ありがとうございます。
1点不明な点があったのですが、スマートニュースにはアドセンスを1枚貼れるようで、そのやり方が分かりませんでした。
また、スマートフォーマットに準拠すると関連記事への内部リンクが貼れないのですが、文中の一部分やリンクカードのみ送らない方法があるのかどうかがあると私のようなCSSに詳しくないブロガーは助かるのではないかと感じました。
お時間があるときにでも追記していただけましたら幸いです。
竹山