Dulu, dulu sekali saya berkeinginan untuk membuat sebuah halaman blog yang mana di dalamnya memuat post yang sudah ditulis oleh teman-teman saya di blog mereka. Saat itu saya masih buta sekali dengan WordPress. I was just a user who installed WordPress then installed theme and some plugins. Kemudian di sela liburan panjang kemarin saya mencoba membuat sebuah WordPress Theme, kemudian berkeinginan untuk merealisasikan keinginan yang sudah lama terpendam itu.
Setelah melakukan pencarian lewat search engine dengan keyword yang bermacam-macam dan berselancar sini-situ, akhirnya jadilah sebuah page template khusus untuk mendukung fitur RSS Feed. Hasilnya dapat dilihat di `halaman Friends`

WordPress
Retrieve Feeds
Awalnya saya include terlebih dahulu fungsi-fungsi yang mendukung RSS Feed. Ingat! deklarasikan ini sebelum memanggil get_header1
get_template_part(ABSPATH . WPINC . '/feed.php');
Setelah itu saya mengambil seluruh tautan (link) yang sudah dibuat di dalam kategori RSS. Daftar tautan ini bisa dikonfigurasi di Administration Panel — Links — All Links. Jika belum ada kategori RSS, maka sebaiknya kategori RSS dibuat terlebih dahulu. Oia jangan lupa untuk mengisi kolom Advanced — RSS Adress dengan tautan feed yang spesifik. Misalnya http://blog.amyunus.com/feed/.
// retrieve links array from rss category
$feeds = get_bookmarks( array ( 'category_name' => 'rss' ) );
//Initialize array
$first_items = array();
// looping each item in array
foreach ( $feeds as $url )
{
// retrieve feed from a link
$feed = fetch_feed( $url->link_rss );
// limit max entry to be retrieved and put in array
$items_per_feed = 1;
for ( $x = 0; $x < $feed->get_item_quantity( $items_per_feed ); $x++ )
{
$first_items[] = $feed->get_item( $x );
}
// we don't need this array anymore so better to release the memories
unset( $feed );
}
Sorting Feeds
Nah, pengambilan entry feed dari setiap blog sudah dilakukan. Tapi page template masih belum sempurna, karena sebaiknya kita mengurutkan seluruh entry tersebut berdasarkan tanggal terbit. Pengen curcol dikit, saya ngutak-ngatik ini beberapa jam dengan menggunakan beberapa array hack dsb. Dan ternyata class SimplePie yang dibawa WordPress sudah bisa melakukan hal tersebut dengan (sangat) mudah (sekali). :’( Berikut kodenya
// Sort feed item by date
function sort_items( $a, $b )
{
return SimplePie::sort_items( $a, $b );
}
usort( $first_items, "sort_items" );
Print Feeds
Setelah itu barulah kita mencetaknya ke layar
// Looping array of entries
<?php foreach( $first_items as $item ) : ?>
<li>
<div>
// print the title
<a href='<?php echo esc_url( $item->get_permalink() ); ?>' rel="nofollow">
<div><h3><?php echo esc_html( $item->get_title() ); ?></h3></div>
</a>
// print the meta such as publish date and author
<div class="entry-meta"><em>
<?php
echo ' Posted at '. $item->get_date( get_option( 'date_format' ) );
if( $author = $item->get_author() ){
echo ' by '. $author->get_name();
}
?>
</em></div>
// print the content of entry, I prefer to show the summary
<div class="entry-content">
<?php echo $item->get_description(); ?>
</div>
</div>
</li>
<?php endforeach; ?>
Bonus: Retrieve first image of each entry
Nah page template itu hanya menampilkan judul, nama penulis, tanggal, dan summary sebuah post. Jika ingin mendapatkan gambar yang ada di dalam post, bisa dilakukan dengan menyematkan kode berikut ke dalam page template2
if( get_first_image_url( $item->get_content() ) != '' ) {
echo '<div><img src="' . get_first_image_url( $item->get_content() ) . '" /></div>';
}
Kemudian menambahkan kode berikut ke dalam berkas functions.php
function get_first_image_url( $html )
{
if ( preg_match( '/<img.+?src="(.+?)"/', $html, $matches ) ) {
return $matches[1];
}
else return '';
}
Oke. Segitu dulu tutorial dari saya. Do you have a better code? let us know yours
Sumber referensi inspirasi kode
1Limit the number of items per feed to be used with Multifeeds
2Using SimplePie to take only first image from RSS feed
Facebook
Twitter
Google+
Email
Sep 16, 2011 @ 11:03:16
tetep bingung. hehehe. tapi hasil jadinya di ‘halaman friends’ jadi menarik yah.