Latest News in Magento
43 Comments 31st MAR 2009 | Posted by Tomislav Bilic in Magento

You would like to have a eCommerce power of Magento, but also have a blog to empower your business? In this case, you probably know that Magento doesn’t have some article manager in the box. Many clients seek for supplementary solution like WordPress to accomplish this goal. Ok, so you created a blog on same or different domain and you would like those articles to appear somewhere in Magento (probably sidebar). This article will explain how to do it.
Step 1
Let’s create a file called latest_news.phtml in app/design/frontend/default/[your_theme]/template/callouts/latest_news.phtml
Now we will create a PHP block that will display the list of articles from RSS feed. We will use Inchoo RSS for demonstration purposes. In your scenario, replace it with your own valid RSS URL.
< ?php $channel = new Zend_Feed_Rss('http://feeds.feedburner.com/Inchoo'); ?>
<div class="block block-latest-news">
<div class="block-title">
<h2>< ?php echo $this->__('Latest News') ?></h2>
</div>
<div class="block-content">
<ol id="graybox-latest-news">
< ?php foreach ($channel as $item): ?>
<li><a href="<?php echo $item->link; ?>">< ?php echo $item->title; ?></a></li>
< ?php endforeach; ?>
</ol>
</div>
</div>
Step 2
Now, we should decide where to place it. I assume you already know how Magento blocks and references work. Let’s assume you would like to place it in right column by default for whole catalog. In this case open your app/design/frontend/default/[your_theme]/layout/catalog.xml file and under “default” tag update “right” reference with something similar.
<reference name="right"> <block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/> <block type="core/template" name="right.permanent.callout" template="callouts/right_col.phtml"/> <block type="core/template" name="right.latest.news" template="callouts/latest_news.phtml"/> </reference>
That’s it. You should be able to see the list of articles from RSS feed with the URLs. Hope this will help someone.
To post code in comments, place your code inside [code] and [/code] tags.


















March 31st, 2009 at 20:41
Great post Tomislav.
It’s simple and effectvie.
And a good example of library reutilization.
March 31st, 2009 at 23:14
That’s GREAT!
I was looking for this for a long time!
Many thanks Tom
April 2nd, 2009 at 11:13
Hi!
How can I set the number of links that I would show? Thanks
April 7th, 2009 at 10:18
Genius! Thanks for posting this.
If anyone wants to limit the number of loops, then you can use a counter and break out of the loop when you reach the limit e.g.
April 9th, 2009 at 9:02
Hi Vicky, I’m not a programmer.. so, can you explain it better? thanks for interest
April 20th, 2009 at 14:31
genius! I have been looking for this for a while now
thanks
andy
April 20th, 2009 at 23:29
This is great but is kinda vague with exactly what to do in the second step any suggestions?
April 29th, 2009 at 16:19
It gives me an error:
Notice: Undefined variable: item in /home/dolle/public_html/app/design/frontend/default/default/template/callouts/blog.phtml on line 10
Don’t know whats going on there?
Please someone help
May 6th, 2009 at 7:32
Yea it doesnt work for me as well. I get the error when I only enter
I dont have a form.mini so I left that out but still no good.
May 8th, 2009 at 15:47
Tomislav – thanks for this code. It saved me a lot of work and research
@John – If you copy-paste the code from above the chars “<” and “?” are separated by space while they need to be joined so “<?”. I had the same problem and I got it solved by modifying the code in my file.
@Jae Jun – don’t copy whole part, but only
and put it under .
Other rows are the author’s code/tempate and probably he make/add some other plugins not related to the example with feed.
Cheers again
May 18th, 2009 at 11:51
its not displaying in the frontpage and none displaying else where
June 3rd, 2009 at 19:08
Thanks Nik. I got it working by keeping the < and ? together.
I just have to work on the formatting now so that it fits with the my theme.
June 4th, 2009 at 0:30
hi, I tried to show with the title, a little part of the contet, if I put this code:
content; ?>
but it show all the post content.
Ther is a simple way to show only the excerpt or a limited number of words?
Many thanks!
July 5th, 2009 at 13:09
Doesn’t work for me either. I get a whole lot of errors.
July 5th, 2009 at 21:12
Any idea how can I limit this to only the last 5 articles?
July 12th, 2009 at 17:32
Yeah! Does someone knows how to do that. It’s a problem I also have. Thanks!
July 16th, 2009 at 7:21
the code may somewhere incorrect,fail to install,eg.the : ought to be ;
August 5th, 2009 at 21:02
you can show on the home page if you insert “reference block” in app/design/frontend/default/[your_theme]/layout/page.xml under “default section”
October 21st, 2009 at 8:58
How can we limit this for only 5 articles? There are a few people that ask this, anyone?
I did something like this, but not implemented right.
$limit = 2;
foreach ($array as $key => $value) {
IF($key == $limit) {
// stop loop here
}
}
anyone?
October 29th, 2009 at 23:49
Yes! I too want to know how to limit the number of feeds to be displayed!
Thanks for this script! It works brilliantly!
October 30th, 2009 at 7:27
Thanks dude! it works for me.
October 30th, 2009 at 10:36
hi
Pls help, its does not work for me either, showing bugs here, can you tell me what is the issues….
Notice: Undefined variable: item in D:\xampp\htdocs\famco4\app\design\frontend\default\default\template\callouts\latest_news.phtml on line 36
Trace:
#0 D:\xampp\htdocs\famco4\app\design\frontend\default\default\template\callouts\latest_news.phtml(36): mageCoreErrorHandler(8, ‘Undefined varia…’, ‘D:\xampp\htdocs…’, 36, Array)
#1 D:\xampp\htdocs\famco4\app\code\core\Mage\Core\Block\Template.php(144): include(‘D:\xampp\htdocs…’)
#2 D:\xampp\htdocs\famco4\app\code\core\Mage\Core\Block\Template.php(176): Mage_Core_Block_Template->fetchView(‘frontend\defaul…’)
#3 D:\xampp\htdocs\famco4\app\code\core\Mage\Core\Block\Template.php(193): Mage_Core_Block_Template->renderView()
#4 D:\xampp\htdocs\famco4\app\code\core\Mage\Core\Block\Abstract.php(643): Mage_Core_Block_Template->_toHtml()
December 14th, 2009 at 6:24
I’m having trouble getting this to work with an atom feed (.xml)
The RRS feed I’m trying to dispay is something like:
http://blog.foobar.com/interestin_stuff/atom.xml
I have the example working beautifully with your Inchoo feed, but it blows up when I try to use the ATOM. Any ideas?
December 14th, 2009 at 6:36
Nothing like a dose of RTFM!!!
http://framework.zend.com/manual/en/zend.feed.consuming-atom.html
January 8th, 2010 at 3:33
Works like a charm. Can’t find how to limit the amount of items consumed, but you can do that from your wordpress or blog backend by limiting how many items are pushed.
January 13th, 2010 at 18:10
Well that didn’t post correctly…
Sorry, see below (not sure why the last didn’t show up, so I changed the open/close tags characters to be “[" and "] respectively.)
[ol id="graybox-latest-news"]
[?php $i = 0; ?]
[?php foreach ($channel as $item): ?]
[?php if($i < 5){?]
[li][a href="[?php echo $item->link; ?]“][ ?php echo $item->title; ?][/a][/li]
[?php $i++; ?]
[?php } ?]
[?php endforeach; ?]
[/ol]
January 26th, 2010 at 11:37
Nice tutorial for RSS
January 30th, 2010 at 13:47
Hi,
Nice Tutorials but i need the code for show only 3 article, so please if any one now kindly reply me .
My Code is some thing below but it didn’t work fine
<?php if($i
<a href="link; ?>“>title; ?>
thanks,
Malik Adeel Ishfaq
January 30th, 2010 at 13:48
sorry above code is not completed
this one
[ol id="graybox-latest-news"]
[?php $i = 0; ?]
[?php foreach ($channel as $item): ?]
[?php if($i [ 5){?]
[li][a href="[?php echo $item-]link; ?]“][?php echo $item-]title; ?][/a][/li]
[?php $i++; ?]
[?php } ?]
[?php endforeach; ?]
[/ol]
February 25th, 2010 at 23:39
make sure if you copy this code that it doesnt look like this: < ?php
If it does change all those tags to <?php
February 26th, 2010 at 8:43
Tom you say right but it for example because editor didn’t accept the tags if i write code in the editor execute it.
March 10th, 2010 at 11:06
hi, I tried to show with the title, a little part of the contet, if I put this code:
content; ?>
but it show all the post content.
Ther is a simple way to show only the excerpt or a limited number of words?
Many thanks!
March 10th, 2010 at 17:05
Have you tried something like:
substr($item->content, 0, 25)
Where 25 is the number of characters you are displaying from the content?
March 11th, 2010 at 13:42
Hi, thanks for that – got it working. Just wondering has anyone tried adding a date? Or does anyone know how?
March 11th, 2010 at 16:45
You’ll have to look at the xml that makes up your feed. Most feeds have a and an node. Find the node that has the date info you want to show and use the $item-> syntax to display it.
March 11th, 2010 at 16:47
In reference to the post above, the second sentence is supposed to read: “Most feeds have a ‘published’ and an ‘updated’ node.”
April 12th, 2010 at 4:24
Wow, thank you very well for this! I search high and low and try stuffs much harder with no lucks! But you, you save the day! Thank you, thank you very well!
May 7th, 2010 at 5:56
Thanks for the info, you can embed this template into a CMS page or block too with code like this:
{{block type=’core/template’ name=”footer.rss” template=’page/html/rss.phtml’ }}
(assuming you put your rss.phtml into templates/page/html)
July 1st, 2010 at 9:17
I have managed with the help from reading the guys on this page get this working fine but the last thing to solve is bugging me, so I am after some help please.
I have included Publishing date and limited the description succesfully but failed to get the two below lines to work to limit the number of feeds to show in the home page to 3.
[?php $item = 0; ?]
[?php if(++$item == 3) {break;}?]
My code as it stands….
[?php $channel = new Zend_Feed_Rss('http://www.my-site.co.uk/news/rss.php'); ?>div id="rss-block-container">
[div class="rss-block-title"]
[h2][?php echo $this->__('Latest News') ?][/h2]
[/div]
[div id="rss-content"]
[ol id="rss-content"]
[?php $item = 0; ?]
[?php foreach ($channel as $item): ?]
[li id="rss-content"][a href="[?php echo $item->link; ?]“][?php echo $item->title; ?][/a][?php echo substr($item->description,0,500); ?][?php echo $item->pubDate; ?][/li]
[?php endforeach; ?]
[/ol]
[?php if(++$item == 3) {break;}?]
[/div]
[/div]
thanks in advance…….
July 1st, 2010 at 14:05
@Tim – as it stands, your code isn’t checking for (++$item ==3) until after the loop is done running (and all articles are printed.)
Move the [?php if(++$item == 3) {break;}?] to the line above the [?php endforeach; ?] and it should work. This way, each iteration through the loop, you’re checking to see how many articles have been run and you’ll know when to stop (or ‘break’ out of the foreach loop.)
July 1st, 2010 at 16:23
Thank you for your help Daniel,
I have tried this and it did not work, any other idea’s …..
July 1st, 2010 at 17:51
on closer examination, it looks like you’re using $item as a counter variable, then replacing it as an actual item object in the foreach.
Change
[?php $item = 0; ?]
to
[?php $counter = 0; ?]
and make the same change to
[?php if(++$item == 3) {break;}?]
making it
[?php if(++$counter == 3) {break;}?]
obviously I used the variable name ‘counter’ to be explicit, but you can name it anything you want.
July 1st, 2010 at 22:43
You Are a Star thank you…………..works perfectly!