Displaying a list of WordPress recent posts in Magento

Written by Kushagra Daharwal - Magento Tutorials, Wordpress - No Comments

I’ve recently had the opportunity to dynamically integrate the latest posts from a client’s WordPress blog into their magento front page and wanted to post a short piece about how it was done. This will effectively allow you to pull in the text only from the latest post in WordPress and link that text to the post it came from. So, let’s begin.

Magento wordpress integration

Magento WordPress integration

Magento Installation:

The first step is the module integrating the two. The best thing I’ve found for this is FishPig’s Magento WordPress Integration. This module provides considerably more functionality than I’ll be discussing here but is still the fastest way to get the job done.

OK, to start your Magento installation, go to the link below and click “Install Now”. This won’t actually install the extension but will give you the extension key.

http://www.magentocommerce.com/magento-connect/wordpress-integration.html

From here, you’ll need to log into your Magento store and go to System >> Magento Connect >> Magento Connect Manager. Once you’re there, you should be able to go to the “Install New Extensions” area, past your extension key, and click “Install” to install the extension.

Configuration:

Now that you’ve got the fishpig extension installed, you’ll need to configure it. Log into the Magento admin and go to System >> Configuration and find the Word-press section. From here you’ll need to go to Setup >> Database and enter the information for your Word-press database.

It’s worth mentioning that, if you’re using separate databases for Magento and WordPress, you’ll need to grant select privileges on your Magento database to your WordPress user or you may run into trouble.

Integration:

OK. We’re configured! It’s time to integrate your latest post into the front page. Fishpig comes  with a series of pre-built templates to help you present Word-press content in Magento, most of which will be located here by default. I recommend copying this directory to your custom theme for modification in Magento.

/app/design/front-end/base/default/template/WordPress/sidebar/widget

So the closest template file to what we need is actually the category posts.phtml file. This file had too much information in it for my purposes but is a good starting point for anything you’d like to do. To meet my needs, only displaying a small portion of the text, I created a custom template titled frontposts.phtml with what you see below. You’ll need to place this file inside of /app/design/front-end/base/default/template/WordPress/sidebar/widget or the equivalent in your theme.

[codesyntax lang="php"]

<?php
/**
* @category Fishpig
* @package Fishpig_Wordpress
* @license http://fishpig.co.uk/license.txt
* @author Ben Tideswell <help@fishpig.co.uk>
*/
?>
<?php $posts = $this->getPosts() ?>
<?php if (count($posts) > 0): ?>
<div>
<ul id="<?php echo $this->getListId() ?>">
<?php foreach($posts as $post): ?>
<?php $post->setExcerptSize($this->getExcerptLength()) ?>
<li>
<?php if ($this->canDisplayExcerpt()): ?>
<a href="<?php echo $post->getPermalink() ?>" title="<?php echo $this->htmlEscape($post->getPostTitle()) ?>">
<p><?php $content = $post->getPostExcerpt(); $content = strip_tags($content); echo substr($content, 0, 250); ?> ...</p>
</a>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script>
<?php if ($this->canShowPager()): ?>
<?php echo $this->getPagerHtml() ?>
<?php endif; ?>
</div>
<?php endif; ?>

[/codesyntax]

Now that we’ve created the file, we’ll actually need to include it in the site. To do that, pull up the CMS page you want to pull posts into and add the following text. This will display a short version of the most recent post and link the text to the post. Remember to replace whatever’s needed to match your theme and enjoy!

[codesyntax lang="php"]

{{block type="wordpress/sidebar_widget_posts" name="wordpress.widget.recent_posts" post_count="1" title="Latest Blogs" excerpt="on" excerpt_length="250" date="off" comment_num="off" template="wordpress/sidebar/widget/frontposts.phtml"}}

[/codesyntax]


Comments are closed.