LoginSignup
11
8

More than 3 years have passed since last update.

WordPressにグノシー向けのフィードを追加

Last updated at Posted at 2017-11-09

WordPressにスマートニュース向けのフィードを追加
に続いて。

GunosyFeed Ver.3 仕様書/手順書
GunosyFeedValidator

/path/to/theme/functions.php
//フィード追加
add_action('init', function() {
    add_feed('gunosy', function() { get_template_part('/feed/gunosy'); });
});

//クエリ変更
add_action('pre_get_posts', function($query) {
    if ( is_admin() ) return $query;
    if ( $query->is_main_query() && $query->is_feed('gunosy') ) {
        //投稿タイプ変更or追加
        $query->set('post_type', ['post', 'any']);
        //削除に対応する場合
        $query->set('post_status', ['publish', 'trash']);
    }
    return $query;
});

リライトルールを追加するので
設定>パーマリンク設定で更新。

※ATOM準拠

/path/to/theme/feed/gunosy.php
<?php
header('Content-Type: ' . feed_content_type('atom') . '; charset=' . get_option('blog_charset'), true);
$more = 1;

echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
do_action( 'rss_tag_pre', 'atom' );
?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en" xmlns:gnf="http://assets.gunosy.com/media/gnf" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
    <title><?php echo get_option('blogname');?></title>
    <link type="text/html" href="<?php echo home_url('/');?>" rel="alternate"/>
    <subtitle><?php echo get_option('blogdescription');?></subtitle>
    <updated><?php echo mysql2date('Y-m-d\TH:i:s\Z', get_lastpostmodified('GMT'), false); ?></updated>
    <rights>【コピーライト】</rights>
    <logo>【正方形ロゴ】</logo>
    <gnf:wide_image_link>【縦44px横長ロゴ】</gnf:wide_image_link>
<?php
while ( have_posts() ) : the_post(); global $post;
?>
    <entry>
        <title><![CDATA[<?php the_title_rss();?>]]></title>
        <link type="text/html" href="<?php the_permalink_rss();?>" rel="alternate"/>
        <id><?php the_ID();?></id>
        <published><?php echo get_post_time(DATE_RFC3339, true);?></published>
        <updated><?php echo get_post_modified_time(DATE_RFC3339, true);?></updated>
        <summary><?php the_excerpt_rss();?></summary>
        <content><![CDATA[
<?php if ( has_post_thumbnail() ):?>
            <figure>
                <img src="<?php echo get_the_post_thumbnail_url($post->ID, 'large');?>">
                <figcaption><?php echo get_the_post_thumbnail_caption($post->ID);?></figcaption>
            </figure>
<?php endif;?>
<?php the_content_feed('atom');?>
        ]]></content>
        <gnf:category>any</gnf:category>
<?php
$categories = get_the_category($post->ID);
if ( !empty($categories) ):
    $cats = [];
    foreach ($categories as $category) {
        if ( !in_array($category->name, $cats) ) $cats[] = $category->name;
        if ( count($cats) >= 10 ) break;
    }
?>
        <gnf:keyword><?php echo implode(',', $cats);?></gnf:keyword>
<?php endif;?>
<?php if ( has_post_thumbnail() ):?>
        <link rel="enclosure" type="image/jpeg" title="<?php echo get_the_post_thumbnail_caption();?>" href="<?php echo get_the_post_thumbnail_url($post->ID, 'large');?>" />
<?php endif;?>
        <media:status state="<?php echo $post->post_status=='publish' ? 'active' : 'deleted';?>" />
<?php do_action( 'atom_entry' );?>
    </entry>
<?php endwhile;?>
</feed>

content内にスクリプトが使えないので
twitterやinstagramのウィジェット入れてる場合は<script>だけ除去する処理を入れる。

アナリティクスのタグとか関連記事などは適宜追記を。

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8