{"id":130,"date":"2006-04-11T14:55:38","date_gmt":"2006-04-11T14:55:38","guid":{"rendered":"http:\/\/dev.gregbueno.com\/wp\/sakufu\/2006\/04\/11\/adding_channel\/"},"modified":"2006-04-11T14:55:38","modified_gmt":"2006-04-11T14:55:38","slug":"adding_channel","status":"publish","type":"post","link":"https:\/\/gregbueno.com\/sakufu\/2006\/04\/11\/adding_channel\/","title":{"rendered":"Adding channel info to Magpie aggregation"},"content":{"rendered":"<p>This <a href=\"http:\/\/rss.gregarius.net\/MagpieRSS___PHP_RSS_Parser\/2004\/10\/26\/Aggregate_and_Collate_with_Magpie\">mirror<\/a> of an article describes how to aggregate and collate RSS and Atom feeds with <a href=\"http:\/\/magpierss.sourgeforge.net\/\">MagpieRSS<\/a>. It&#8217;s really effective, but in following the steps exactly laid out, you lose channel information. When you want to distinguish between different feeds, you may want to include the basics, such as title (the name of the site) and link (the base url of the site.)<\/p>\n<p>So I devised a bit of a hack to append that information to the items array.<\/p>\n<p><!--more--><\/p>\n<p>Here&#8217;s the original code:<\/p>\n<p><span style=\"font-family: courier new, courier, serif\"><\/p>\n<p>$urls = array(&#8216;http:\/\/example.org\/index.rdf&#8217;, &#8230;);<\/p>\n<p>$items = array();<\/p>\n<p>foreach ( $urls as $url ) {<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$rss = fetch_rss($url);<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;if (!$rss) continue;<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$items = array_merge($items, $rss-&gt;items);<\/p>\n<p>}<\/p>\n<p><\/span><\/p>\n<p>What you want to do is append the channel fields to each item of the <tt>$rss-&gt;items<\/tt> array. You&#8217;ll need to drill down to an individual item, append the new fields, then push the resulting array into a buffer, which then gets merged with items from other feeds. It&#8217;s somewhat kind of convulted, given the nested structure of the arrays.<\/p>\n<p>The buffer also needs to be reset each time it&#8217;s used. It&#8217;s also wise to name the indeces for the fields <tt>channel_title<\/tt> and <tt>channel_link<\/tt>, so as not to conflict with the item <tt>title<\/tt> and the item <tt>link<\/tt>.<\/p>\n<p><span style=\"font-family: courier new, courier, serif\"><\/p>\n<p>$urls = array(&#8216;http:\/\/example.org\/index.rdf&#8217;, &#8230;);<\/p>\n<p>$items = array();<\/p>\n<p>$channelItems = $rss-&gt;items;<\/p>\n<p>$completeItems = array();<\/p>\n<p>foreach ($channelItems as $item)<\/p>\n<p>{<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$item[channel_title] = $rss-&gt;channel[title];<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;$item[channel_link] = $rss-&gt;channel[link];<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;array_push($completeItems, $item);<\/p>\n<p>}<\/p>\n<p>$items = array_merge($items, $completeItems);<\/p>\n<p>unset($completeItems);<\/p>\n<p><\/span><\/p>\n<p>Now when you cycle through each item, you can include the channel title and link. The following is an example with Smarty:<\/p>\n<p><span style=\"font-family: courier new, courier, serif\"><\/p>\n<p>{foreach item=feed from=$feed}<\/p>\n<p>{if $feed.dc.creator}{assign var=author value=$feed.dc.creator}<\/p>\n<p>{elseif $feed.author_name}{assign var=author value=$feed.author_name}<\/p>\n<p>{\/if}<\/p>\n<p>&lt;p&gt;<\/p>\n<p>&lt;strong&gt;&lt;a href=&#8221;{$feed.link}&#8221;&gt;{$feed.title}&lt;\/a&gt;&lt;\/strong&gt;&lt;br&gt;<\/p>\n<p>{if $feed.description}{$feed.description}&lt;br&gt;{\/if}<\/p>\n<p>&lt;span style=&#8221;font-size: 10px;&#8221;&gt;&#8211; posted on {$feed.date_timestamp|date_format:&#8221;%m\/%d\/%Y %H:%M:%S&#8221;} |<\/p>\n<p>file under: &lt;a href=&#8221;{<strong>$feed.channel_link<\/strong>}&#8221;&gt;{<strong>$feed.channel_title<\/strong>}&lt;\/a&gt;&lt;\/span&gt;&lt;br&gt;<\/p>\n<p>&lt;\/p&gt;<\/p>\n<p>{\/foreach}<\/p>\n<p><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you want to distinguish between different feeds, you may want to include the basics, such as title and link. So I devised a bit of a hack to append that information to the items array.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[6],"tags":[],"class_list":["post-130","post","type-post","status-publish","format-standard","hentry","category-technophilia-professional"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p4Bkjq-26","_links":{"self":[{"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/posts\/130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/comments?post=130"}],"version-history":[{"count":0,"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/posts\/130\/revisions"}],"wp:attachment":[{"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregbueno.com\/sakufu\/wp-json\/wp\/v2\/tags?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}