쇼 정적 배너 또는 카테고리에 제품 페이지
-
13-12-2019 - |
문제
나는 다음과 같은 코드 product/view.phtml
:
<?php
if(Mage::registry('current_category')):
$_category=Mage::registry('current_category');
$_helper = Mage::helper('catalog/output');
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}?>
<?php if($_imgUrl) { ?>
<!-- Category has Image uploaded -->
<?php echo $_imgHtml ?>
<?php }else{ ?>
<!-- Category has NO Image uploaded, show static block -->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php }?>
<?php endif;?>
이것은 현재 보여주거나 범주 배너 또는 정적 배너는 경우 카테고리가 없는 배너입니다.까지,남녀노소 누구나 좋아합니다 제품 페이지를 방문하이 직접 다음 없는 배너가 표시됩니다.
할 수 있는 사람을 주시기 바랍 수정을 제안하는 경우에는 사용자가 방문하는 제품을 직접적으로(즉지 않을 방문하여 범주)다음의 정적 배너('cms_top_banner')이 표시됩니다.
해결책
여기에 문제가있을 때,로드하는 제품 페이지에 직접 Mage::registry('current_category')
환 NULL
따라서 아무것도 얻을 것이 실행되는 코드,이후 모든 코드에 있는 부모는 경우 루프 검사입니다.
아래에 사용할 수 있습 코드입니다.
<?php
$_imgHtml = '';
$_imgUrl = false;
//check whether a valid category exist
if(Mage::registry('current_category')) {
//grabing category banner if any
$_category=Mage::registry('current_category');
$_helper = Mage::helper('catalog/output');
if ($_imgUrl = $_category->getImageUrl()) {
//prepare banner html
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
}
//check a valid image url exist
if ($_imgUrl) {
echo $_imgHtml;
} else {
echo $this->getLayout()->createBlock('cms/block')
->setBlockId('cms_top_banner')
->toHtml();
}
?>
이 경우,유효한 범주가 존재하지 않습니다,그것은 당신의 기본 배너 어떤 경우에도 빠지지 않았습니다.
다른 팁
이 코드를 시도 할 수 있습니다. 카테고리가없는 경우 정적 배너가 표시됩니다.
<?php
if(Mage::registry('current_category')):
$_category=Mage::registry('current_category');
$_helper = Mage::helper('catalog/output');
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) {
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
}
?>
<?php if($_imgUrl) { ?>
<!-- Category has Image uploaded -->
<?php echo $_imgHtml ?>
<?php }else{ ?>
<!-- Category has NO Image uploaded, show static block -->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php }?>
<?php else: ?>
<!-- No category available, show static block -->
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php endif; ?>
. 이
를 사용해보십시오 <?php if(Mage::registry('current_category')):
$_category=Mage::registry('current_category');
$_helper = Mage::helper('catalog/output');
$_imgHtml = '';
if ($_imgUrl = $_category->getImageUrl()) :
$_imgHtml = '<p class="category-image"><img src="'.$_imgUrl.'" alt="'.$this->escapeHtml($_category->getName()).'" title="'.$this->escapeHtml($_category->getName()).'" /></p>';
$_imgHtml = $_helper->categoryAttribute($_category, $_imgHtml, 'image');
endif;?>
<?php if($_imgUrl) : ?>
<?php echo $_imgHtml ?>
<?php else: ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php endif;?>
<?php else:?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('cms_top_banner')->toHtml(); ?>
<?php endif;?>
. 제휴하지 않습니다 magento.stackexchange