2
votes

We installed Wordpress on our server and the Fishpig extension that integrates Wordpress and Magento. We want to make use of the extension's ability to associate blog posts and products by adding related posts to a new tab on the frontend product page (where the product description and upsell products are). I got all of that to work--there is a new tab on the product pages titled "Related Blog Posts" and when selected, it displays the post titles and excerpts exactly how I wanted it to. The problem is, that tab displays even when there are no related blog posts. How do I hide it when it's empty?

There's probably something simple that I'm missing. Here's how I added the tab:

1) This file app/design/frontend/base/default/layout/wordpress.xml establishes which block the related posts are displayed in on the frontend product page. In this file, near the bottom, I changed the reference name from product.info.additional to *related_blog_posts*.

2) To the file: app/design/frontend/default/{template}/layout/catalog.xml near line 210, I placed the following code. I put it between similar code portions for the product description and the product upsells.

<action method="addTab" translate="title" module="catalog"><alias>related_blog_posts</alias><title>Related Blog Posts</title><block>catalog/product_list_relatedposts</block><template>catalog/product/list/relatedposts.phtml</template></action>

3) Added a new file: app/code/local/Mage/Catalog/Block/Product/List/relatedposts.php Added the following code to this file:

class Mage_Catalog_Block_Product_List_Relatedposts extends Mage_Core_Block_Template
{
    protected $_list;
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('catalog/product/view/additional.phtml');
    }
    public function getChildHtmlList()
    {
        if (is_null($this->_list)) {
            $this->_list = array();
            foreach ($this->getSortedChildren() as $name) {
                $block = $this->getLayout()->getBlock($name);
                if (!$block) {
                    Mage::exception(Mage::helper('catalog')->__('Invalid block: %s.', $name));
                }
                $this->_list[] = $block->toHtml();
            }
        }
        return $this->_list;
    }
}

4) Added a new file: app/design/frontend/default/{template}/template/catalog/product/list/Relatedposts.phtml and added the following code to this file:

<?php foreach ($this->getChildHtmlList () as $_html): ?>
    <div class="collateral-box">
        <?php echo $_html ?>
    </div>
<?php endforeach; ?>

5) In the file app/design/frontend/base/default/template/wordpress/post/associated/list.phtml I changed the following code:

<ul>
<?php foreach($posts as $post): ?>
    <li>
        <a href="<?php echo $post->getPermalink() ?>" title="<?php echo $this->escapeHtml($post->getPostTitle()) ?>"><?php echo $this->escapeHtml($post->getPostTitle()) ?></a>
    </li>
<?php endforeach; ?>
</ul>

To:

<div class="related-posts">
    <?php foreach($posts as $post): ?>
        <h3><a href="<?php echo $post->getPermalink() ?>" title="<?php echo $this->escapeHtml($post->getPostTitle()) ?>"><?php echo $this->escapeHtml($post->getPostTitle()) ?></a></h3>
        <?php $post->setExcerptSize($this->getExcerptLength()) ?>
        <p class="related-post-excerpt"><?php echo $post->getPostExcerpt() ?></p>
    <?php endforeach; ?>
</div>

This last change adds an excerpt to each related post instead of just displaying the title.

6) Cleared caches and recompiled the site.

Summary: The new tab appears on the frontend product page and the related blog posts are appearing within the tab as they should. The tab, however, displays even when there are no related blog posts for that product. I have tried multiple ways of wrapping the code in Relatedposts.phtml in if/count conditions but I can't get anything to work. How do I prevent my new tab from appearing when there is no content?

1

1 Answers

0
votes

I would try something like this or like you had mentioned some sort of count, in

app/design/frontend/default/{template}/template/catalog/product/list/Relatedposts.phtml

<?php 
if($this->getChildHtmlList()): ?>
    <?php foreach ($this->getChildHtmlList () as $_html): ?>
        <div class="collateral-box">
            <?php echo $_html ?>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

You also need to ensure that this block is not returning any white space either because it will be interpreted as content and create the tab. I also had a custom tab and this was the issue for me. If you are having trouble you should Zend_Debug::dump() the $this->getChildHtmlList() and see what is getting generated.