Latest News in Magento

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

Latest News 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.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 43 comments (Add Yours +)

  • Great post Tomislav.
    It’s simple and effectvie.
    And a good example of library reutilization.

  • That’s GREAT!
    I was looking for this for a long time!
    Many thanks Tom :P

  • Hi!
    How can I set the number of links that I would show? Thanks

  • 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.

  • Hi Vicky, I’m not a programmer.. so, can you explain it better? thanks for interest

  • genius! I have been looking for this for a while now

    thanks

    andy

  • This is great but is kinda vague with exactly what to do in the second step any suggestions?

  • 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 :)

  • Jae Jun Says

    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.

  • 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

  • its not displaying in the frontpage and none displaying else where

  • 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.

  • 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! :)

  • Doesn’t work for me either. I get a whole lot of errors.

  • Any idea how can I limit this to only the last 5 articles?

  • Yeah! Does someone knows how to do that. It’s a problem I also have. Thanks!

  • the code may somewhere incorrect,fail to install,eg.the : ought to be ;

  • 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”

  • 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?

  • Yes! I too want to know how to limit the number of feeds to be displayed!

    Thanks for this script! It works brilliantly!

  • Thanks dude! it works for me.

  • 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()

  • 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?

  • 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.

  • 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]

  • Nice tutorial for RSS

  • 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

  • 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]

  • make sure if you copy this code that it doesnt look like this: < ?php

    If it does change all those tags to <?php

  • Tom you say right but it for example because editor didn’t accept the tags if i write code in the editor execute it.

  • 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! :)

  • Have you tried something like:

    substr($item->content, 0, 25)

    Where 25 is the number of characters you are displaying from the content?

  • Hi, thanks for that – got it working. Just wondering has anyone tried adding a date? Or does anyone know how?

  • 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.

  • In reference to the post above, the second sentence is supposed to read: “Most feeds have a ‘published’ and an ‘updated’ node.”

  • 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!

  • 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)

  • 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…….

  • @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.)

  • Thank you for your help Daniel,
    I have tried this and it did not work, any other idea’s …..

  • 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.

  • You Are a Star thank you…………..works perfectly!

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009