show associated blog post/blog category on magento category page using fishpig

I was exploring Magento's Fishpig extension and found an interesting way to bind blog post & blog categories to magento’s categories. But, I am not getting how to show on the frontend of magento category page.

I guess its in built functionality of Fishpig module.

Read More

I tried something using following code:

<catalog_category_view>
<reference name="left">
<block type="wordpress/post_associated" name="wordpress_posts_associated" template="wordpress/post/associated.phtml" after="-">
<action method="setTitle" translate="title" module="wordpress">
<title><![CDATA[Related Blog Posts]]></title>
</action>
<action method="setEntity">
<type><![CDATA[category]]></type>
</action>
</block>
</reference>
</catalog_category_view>

Related posts

Leave a Reply

3 comments

  1. If you wanted to display the direct associations against a category, you would need to retrieve the association from the database and build a post collection manually using the IDs retrieved.

    To expand on Bens comment, the helper Fishpig_Wordpress_Helper_Associations can get associations for you.

    In here you’ll find the function;

    public function getAssociations($type, $objectId, $storeId = null)
    

    If you step through this file you’ll be able to figure out what you need to do, but for ease please an example of it in use below;

    $_helper = Mage::helper('wordpress/associations');
    $_category  = $this->getCurrentCategory();
    $_associations = $_helper->getAssociations('category/category',$_category->getId());
    $_collection = Mage::getResourceModel('wordpress/post_collection')
        ->addIsPublishedFilter();
    

    This will return an array where keys are the WP category IDs and the value is its position within Magento.

    Next you’ll need to flip the keys into values.

    Warning Don’t use array_flip! If you have categories with the same position only the last oe with the same value will be saved.

    Solution It’s a little dirty, but you can then loop through and rebuild the array to be used at a later date;

    if($_associations && $_collection->getSize()){
        $_wpIds = array();
        foreach($_associations as $_id => $_position){
            $_wpIds[] = $_id;
        }
    }
    

    You can filter your collection using the function addCategoryIdFilter($categoryId).
    Unfortunately, it seems this doesn’t accept arrays and if it were to be applied multiple times to your collection then it will return false. Sadly, it seems there isn’t a function within the module where you can filter a collection by an array of category IDs.

    In an ideal world, the ID filter should accept both string and arrays, and if an array you should be able to define an AND/OR parameter. Possibly something for a future release 😉

  2. I found that it is possible to show posts on a category page by adding this code to …category/view.phtml:

     <?php echo Mage::getSingleton('core/layout')
    ->createBlock('wordpress/sidebar_widget_posts')
    ->setTemplate('wordpress/sidebar/widget/categoryposts.phtml')
        ->toHtml() ?>
    

    This will load a list of all posts to your category page whereever you place it. However you need to alter the categoryposts.phtml to fit your needs – hope this helps!

  3. The code in the other answer will show the most recent posts for your whole blog in a sidebar widget, which isn’t what the question asks.

    Although it’s possible to associate blog posts with a Magento category, it isn’t currently possible to display them. This sounds wrong so let me explain.

    When you associate a blog post with a Magento category, you aren’t actually associate these two items together. Instead, this is a quick way to associate the blog post with all products inside that category.

    If you wanted to display the direct associations against a category, you would need to retrieve the association from the database and build a post collection manually using the IDs retrieved.