Skip to content

Instantly share code, notes, and snippets.

@kachi
Created April 15, 2012 08:43
Show Gist options
  • Select an option

  • Save kachi/2391092 to your computer and use it in GitHub Desktop.

Select an option

Save kachi/2391092 to your computer and use it in GitHub Desktop.
<?php
if ( !function_exists('base_rss_feed') ) {
function base_rss_feed($size = 5, $feed = 'http://wordpress.org/news/feed/', $date = false, $cache_time = 1800)
{
include_once ABSPATH . WPINC . '/feed.php';
add_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', "return $cache_time;" ) );
$rss = fetch_feed($feed);
if ( !is_wp_error( $rss ) ) {
$maxitems = $rss->get_item_quantity($size);
$rss_items = $rss->get_items(0, $maxitems);
$i = 0;
$total_entries = count($rss_items);
// HTMLに変換
$html = "<ul class='feedlist'>";
foreach ($rss_items as $item) {
$i++;
// 最後のアイテムにclassを追加
if( $total_entries == $i ) {
$last = " class='last'";
} else {
$last = "";
}
// データ取得
$title = $item->get_title();// タイトル
$link = $item->get_permalink();// リンク
$desc = $item->get_description();// コンテンツ
$date_posted = $item->get_date('Y.m.d');// 日付
// 出力
$html .= "<li id='post-$i'$last>";
$html .= "<h3><a href='$link'>$title</a></h3>";
if( $date == true ) $html .= "<span class='date'>$date_posted</span>";
$html .= "<div class='rss-entry'>$desc</div>";
$html .= "</li>";
}
$html .= "</ul>";
} else {
$html = "取得エラーです。";
}
return $html;
}
}
/** ショートコード */
if( function_exists('base_rss_feed') && !function_exists('base_rss_shortcode') ) {
function base_rss_shortcode($atts) {
extract(shortcode_atts(array(
'size' => '10',
'feed' => 'http://wordpress.org/news/feed/',
'date' => false,
), $atts));
$content = base_rss_feed($size, $feed, $date);
return $content;
}
add_shortcode("rss", "base_rss_shortcode");
}
?>
/* 使い方は[rss size="10" feed="http://kachibito.net/feed" date="true"]という感じ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment