How to display subcategories on category page

In PrestaShop 1.7 classic theme we don't have list of available subcategories (while we browse category page). In this tutorial I want to show you how to display them with fancy nice css styles. In this step by step guide i will change only theme files so it is not necessary to alter core of new PrestaShop. Example of modification you can see below.

subcategories on category page

 

Display subcategories on category page

Firstly we have to alter category.tpl file. This file is responsible for category page view. File is located in classic theme directory, full path to file is: /themes/classic/templates/catalog/listing/category.tpl. Please open it and inside {block} code add this one:

{if isset($subcategories)}
    <!-- Subcategories -->
    <div id="subcategories">
        <p class="subcategory-heading">{l s='Subcategories'}</p>
        <ul class="clearfix">
            {foreach from=$subcategories item=subcategory}
                <li>
                    <div class="subcategory-image">
                        <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}" title="{$subcategory.name|escape:'html':'UTF-8'}" class="img">
                            {if $subcategory.id_image}
                                <img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'category_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}"/>
                            {else}
                                <img class="replace-2x" src="{$img_cat_dir}{$lang_iso}-default-category_default.jpg" alt="{$subcategory.name|escape:'html':'UTF-8'}"/>
                            {/if}
                        </a>
                    </div>
                    <h5><a class="subcategory-name" href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}">{$subcategory.name|truncate:25:'...'|escape:'html':'UTF-8'}</a></h5>
                    {if $subcategory.description}
                        <div class="cat_desc">{$subcategory.description}</div>
                    {/if}
                </li>
            {/foreach}
        </ul>
    </div>
{/if}

This code checks if browsed category page contains subcategories and if so - this code builds a list of available subcategories. List contains image of category and category name. Below i attach full code of modified category.tpl file, if you dont where to paste code - you can just replace contents of your file with code below.

{**
 * 2007-2016 PrestaShop
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 * @author    PrestaShop SA <contact@prestashop.com>
 * @copyright 2007-2016 PrestaShop SA
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * International Registered Trademark & Property of PrestaShop SA
 *}
{extends file='catalog/listing/product-list.tpl'}

{block name='product_list_header'}
    <div class="block-category card card-block hidden-sm-down">
        <h1 class="h1">{$category.name}</h1>
        {if $category.description}
            <div id="category-description" class="text-muted">{$category.description nofilter}</div>
            <div class="category-cover">
                <img src="{$category.image.large.url}" alt="{$category.image.legend}">
            </div>
        {/if}
    </div>
    <div class="text-xs-center hidden-md-up">
        <h1 class="h1">{$category.name}</h1>
    </div>
    {if isset($subcategories)}
        <!-- Subcategories -->
        <div id="subcategories">
            <p class="subcategory-heading">{l s='Subcategories'}</p>
            <ul class="clearfix">
                {foreach from=$subcategories item=subcategory}
                    <li>
                        <div class="subcategory-image">
                            <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}" title="{$subcategory.name|escape:'html':'UTF-8'}" class="img">
                                {if $subcategory.id_image}
                                    <img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'category_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}"/>
                                {else}
                                    <img class="replace-2x" src="{$img_cat_dir}{$lang_iso}-default-category_default.jpg" alt="{$subcategory.name|escape:'html':'UTF-8'}"/>
                                {/if}
                            </a>
                        </div>
                        <h5><a class="subcategory-name" href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}">{$subcategory.name|truncate:25:'...'|escape:'html':'UTF-8'}</a></h5>
                        {if $subcategory.description}
                            <div class="cat_desc">{$subcategory.description}</div>
                        {/if}
                    </li>
                {/foreach}
            </ul>
        </div>
    {/if}
{/block}

 

Add fancy styles to our subcategories list

Now it's time to personalize design of list of subcategories that we created. In this case we will modify theme's css file. It is located in this path: /assets/css/theme.css. Please open it and at the end of file paste these css styles:

/* ************************************************************************************************
									Sub Categories Styles
************************************************************************************************ */
#subcategories {
    border-top: 1px solid #d6d4d4;
    padding: 15px 0 0px 0;
}

#subcategories p.subcategory-heading {
    font-weight: bold;
    color: #333;
    margin: 0 0 15px 0;
}

#subcategories ul {
    margin: 0 0 0 -20px;
}

#subcategories ul li {
    float: left;
    width: 145px;
    margin: 0 0 13px 33px;
    text-align: center;
    height: 202px;
}

#subcategories ul li .subcategory-image {
    padding: 0 0 8px 0;
}

#subcategories ul li .subcategory-image a {
    display: block;
    padding: 9px;
    border: 1px solid #d6d4d4;
}

#subcategories ul li .subcategory-image a img {
    max-width: 100%;
    vertical-align: top;
}

#subcategories ul li .subcategory-name {
    font: 600 18px/22px "Open Sans", sans-serif;
    color: #555454;
    text-transform: uppercase;
}

#subcategories ul li .subcategory-name:hover {
    color: #515151;
}

#subcategories ul li .cat_desc {
    display: none;
}

#subcategories ul li:hover .subcategory-image a {
    border: 5px solid #333;
    padding: 5px;
}

 

That's all, your subcategories list is ready and with fancy design. If you dont see the changes it may be necessary to clear browser cache (because your browser remember old .css file) and clear shop cache.

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim