Jump to content

Hide the subcategory images when image does not exist.


Prestag0od

Recommended Posts

In p1.6 I have put in the comments the code that displays the images of the subcategories at the top of the page in category.tpl so that it does not display them at all.
But I want to display the images in a specific category (category ID) or only in those categories that have images and not display in those that do not have an image. Normally in those categories that do not have a photo, a default one that says no image is displayed. I do not want to display anything when the category does not have a photo.

Can anyone help please?

The code is here

 {if isset($subcategories)}
        {if (isset($display_subcategories) && $display_subcategories eq 1) || !isset($display_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, 'tm_medium_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}" />
							{else}
								<img class="replace-2x" src="{$img_cat_dir}{$lang_iso}-default-medium_default.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" />
							{/if}
						</a>
                   	</div>
					<h5>
                    	<a class="subcategory-name" href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}" title="{$subcategory.name|truncate:25:'...'|escape:'html':'UTF-8'|truncate:50}">{$subcategory.name|truncate:25:'...'|escape:'html':'UTF-8'|truncate:350}</a>
                    </h5>
					{if $subcategory.description}
						<div class="cat_desc">{$subcategory.description}</div>
					{/if}
				</li>
			{/foreach}
			</ul>
		</div>
        {/if}
		{/if}

 

Link to comment
Share on other sites

{else} <img class="replace-2x" src="{$img_cat_dir}{$lang_iso}-default-medium_default.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" style="display:none;" /> {/if}

Link to comment
Share on other sites

1 hour ago, Guest said:

{else} <img class="replace-2x" src="{$img_cat_dir}{$lang_iso}-default-medium_default.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" style="display:none;" /> {/if}

Thanks for helping. I already tried this but even if i delete all this line the default image is still appearing. I dont know from where this default image is comming from!!!!!!

Link to comment
Share on other sites

Hi,

{if $subcategory.id_image && Tools::file_get_contents($link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default'))}
							<img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}" width="{$mediumSize.width}" height="{$mediumSize.height}" />
						{/if}

 

Link to comment
Share on other sites

If i understand well i replace the code

{if $subcategory.id_image}
								<img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'tm_medium_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}" />
							{else}
								<img class="replace-2x" src="{$img_cat_dir}{$lang_iso}-default-medium_default.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" style="display:none;"/>
							{/if}

with your code

{if $subcategory.id_image && Tools::file_get_contents($link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default'))}
							<img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}" width="{$mediumSize.width}" height="{$mediumSize.height}" />
						{/if}

but i have excactily the same result.

Link to comment
Share on other sites

Hi.

1. open ./classes/Category.php

add new function:

public static function categoryImageExists($id_category)
    {
        if (file_exists(_PS_CAT_IMG_DIR_.$id_category.'.jpg')) {$imageExists = '1';} else {$imageExists = '0';} 
        return $imageExists; 
    }

2. open ./themes/default-bootstrap/category.tpl

change to:

{if $subcategory.id_image && Category::categoryImageExists($subcategory.id_category) == '1'}
							<img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}" width="{$mediumSize.width}" height="{$mediumSize.height}" />
						{/if}

3. clear cache

result:

obrazek.png.38d13f1a3ef26614f7a429deb2eb7dec.png

Link to comment
Share on other sites

or category.tpl:

{foreach from=$subcategories item=subcategory}
				<li>
                	<div class="subcategory-image">
                    {if Category::categoryImageExists($subcategory.id_category) == '1'}
						<a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}" title="{$subcategory.name|escape:'html':'UTF-8'}" class="img">					
					       <img class="replace-2x" src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')|escape:'html':'UTF-8'}" alt="{$subcategory.name|escape:'html':'UTF-8'}" width="{$mediumSize.width}" height="{$mediumSize.height}" />
					   </a>
                    {else}
                        <a href="{$link->getCategoryLink($subcategory.id_category, $subcategory.link_rewrite)|escape:'html':'UTF-8'}" title="{$subcategory.name|escape:'html':'UTF-8'}" class="img">					
					       <span alt="{$subcategory.name|escape:'html':'UTF-8'}" style="display:block;min-width:{$mediumSize.width}px;min-height:{$mediumSize.height}px;" />
					   </a>    
                    {/if}
                   	</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}

 

result:

obrazek.png.2be3d89bed9d484c93f66287128ee4b1.png

Link to comment
Share on other sites

I can't advise you for your template I don't know.
I don't understand what you really need.
You have created your own function in PHP and you have the condition in the tpl file.
You can use the condition in tpl as you need.
You need to control HTML and css at least a little.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...