Latest News in Magento

36 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
  • FriendFeed
  • NewsVine
  • Sphinn
  • Tumblr

There are 36 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.”

Leave a Comment

Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009