开发者

Making Category Image a link

is is possible to have the main category image that开发者_如何学编程 you set in the General Information tab made into a link?

It renders out as:

<img src="http://www.example.com/media/catalog/category/example.jpg" alt="Example" title="Example" class="category-image" />

There is perhaps an extension to do this? It just doesn't feel right to have a main banner that is not clickable...


There isn't a set answer , but here was how we were able to do it in Magento 1.7.0.2:

In file app/design/frontend/base/default/template/catalog/category/view.phtml there are these lines that add the image:

if ($_imgUrl = $_category->getImageUrl()) {
    $_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this-    >htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /> </p>';
    $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}

This basically says: if there is an image - create the necessary HTML to display it.

You can recreate these same lines and add an if-statement:

  if ($_imgUrl = $_category->getImageUrl()) { 
//Add this, which reads: if the following text exists in the file name of the category image then create html with a link for that specific text
    if(substr($_imgUrl,-20)=="some-systematic-identification-text"){
        $_imgHtml = '<p class="category-image"><a href="http://www.MY_SITE_URL.com" target="_blank"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></a></p>';
    }
//Add this to check for more text
    else if(substr($_imgUrl,-20)=="some-OTHER-systematic-identification-text"){
         $_imgHtml = '<p class="category-image"><a href="http://www.MY_SITE_URL.com" target="_blank"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></a></p>';
     }
//Otherwise - just add the standard html that was there before we made changes
    else{$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->htmlEscape($_category->getName()).'" title="'.$this->htmlEscape($_category->getName()).'" /></p>';}
//Part of if-category image - if statement          
    $_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
    }

You can copy and paste these lines over the 4 I put at the top of this post and then modify as needed to identify when your file-name has been displayed as a category image and to create the appropriate link that pops out when clicked.


suppose if you want the subcategory image and that category products then you can use below code.

<a href="<?php echo $subcat->getUrlPath();?>"><img src="<?php echo Mage::getBaseUrl()."media/catalog/category/".$subcat->getThumbnail() ?>"/></a>


Do you want to link it to the same category or some other page? You can try this hack...

Category images are loaded by calling

    <p><?php echo $_imgHtml?></p>

in the template\catalog\category\view.phtml file.

You can make the following change and hardcode a link or use a dynamic link.

    <p><a href="#"><?php echo $_imgHtml?></a></p>

If you want to link it to the current category, you can assign the current category URL function to a $variable and use it as your link.

$_category->getUrl()

If you have a specific requirement, comment on the answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜