Jump to content

Easy way to display categories on homepage


Recommended Posts

There's easy way to display categories on homepage.

What i wanted was to show subcategories (title,picture,without products) of desired category on homepage. Using/modifying standard module blockcategories seems to be the right for that purposes.

This module hooks (can be displayed) only on left/right column and footer. So we need to make 2step maintanance:

1) We have to hook this module on homepage content ('displayHome'):

1.1)This can be done by changing blockcategories.php - just add $this->registerHook('displayHome'); in  function hookLeftColumn, for example (this will let you to hook this module to homepage content).

1.2) In Backoffice hook module to homepage content

2) Create tpl-file for displaying at the homepage, and make changes in blockcategories.php to initialize the variables needed for .tpl:

2.1) create in module folder file blockcategories_home.tpl, for example, with something like this:

 

            {if isset($subcategories)}
            <!-- Subcategories -->
            <div id="subcategories" class="block span12">
                <h3 class="title_block">Subcategories</h3>
                <div class="inline_list leo_block_content">
                {foreach from=$subcategories item=subcategory name=subcategories}
                    {if $subcategory@iteration%3==1}
                    <div class="row-fluid">
                    {/if}
                    <div class="span3">
                        <div class="category-container">
                            <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" title="{$subcategory.name|escape:'htmlall':'UTF-8'}" class="img">
                                {if $subcategory.id_image}
                                    <img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')}" alt=""/>
                                {else}
                                    <img src="{$img_cat_dir}default-medium_default.jpg" alt="" />
                                {/if}
                            </a>
                            <h3 class="s_title_block">
                            <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'htmlall':'UTF-8'}" class="cat_name">{$subcategory.name|escape:'htmlall':'UTF-8'}</a>
                                                        </h3>
                        </div>
                    </div>
                    {if $subcategory@iteration%3==0||$smarty.foreach.subcategories.last}
                    </div>
                    {/if}
                {/foreach}
                </div>
            </div>
            {/if}
 

(it's taken from category.tpl from theme folder, you can change it according to what you want).

2.2) Change blockcategories.php adding function

 

public function hookdisplayHome($params)
    {
          if (!$this->isCached('blockcategories_home.tpl', $this->getCacheId()))
        {
        $id_lang = (int)$this->context->language->id;
        $category = new Category(15, $id_lang);
        $subcategories = $category->getSubCategories($id_lang, true);
        $this->context->smarty->assign(array(
            'subcategories' => $subcategories
        ));
        }
        $display = $this->display(__FILE__, 'blockcategories_home.tpl', $this->getCacheId());
        return $display;
    }

 

Where 15 at the row "$category = new Category(15, $id_lang);" means the id of category which subcategories you want to display.

 

2.3) In the function _clearBlockcategoriesCache() add the row "$this->_clearCache('blockcategories_home.tpl');"

 

 

Thats all.

Edited by anriuz (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...