Magento 无法在内容块中使用phtml模板两次
我想设置一个Magento页面,如下所示:Magento 无法在内容块中使用phtml模板两次,magento,Magento,我想设置一个Magento页面,如下所示: <h2>Title</h2> {{block type="catalog/navigation" category_id="5" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}} <h2>Title</h2> {{block type="catalog/navigation" category_id="9" te
<h2>Title</h2>
{{block type="catalog/navigation" category_id="5" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
<h2>Title</h2>
{{block type="catalog/navigation" category_id="9" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
<h2>Title</h2>
{{block type="catalog/navigation" category_id="38" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
$getImageUrl($category)
标题
{{block type=“catalog/navigation”category_id=“5”template=“/catalog/navigation/subcategory.phtml”}
标题
{{block type=“catalog/navigation”category_id=“9”template=“/catalog/navigation/subcategory.phtml”}
标题
{{block type=“catalog/navigation”category_id=“38”template=“/catalog/navigation/subcategory.phtml”}
我的问题是,我调用的模板在一个站点中只能使用一次。如果我想再叫一次,我会得到一张空白页
<div id="categories">
<div class="col_full">
<div class="products-grid row first last odd" >
<?php
function getImageUrl($category)
{
$cur_category=Mage::getModel('catalog/category')->load($category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$url = $this->getCurrentCategory()->getImageUrl();
return $url;
};
//Get the Current Category
$category = $this->getData('category_id');
$_maincategorylisting=Mage::getModel('catalog/category')->getCategories($category);
// Iterate all categories in store
foreach ($_maincategorylisting as $_category):
// If category is Active
if($_category->getIsActive()):
// Load the actual category object for this category
$cur_category = Mage::getModel('catalog/category')->load($_category->getId());
// Load a random product from this category
$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cur_category);
$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(100);
$products->load();
// This a bit of a fudge - there's only one element in the collection
$_product = null;
foreach ( $products as $_product ) {}
if(isset($_product)):
?>
<div class="col-xs-12 col-sm-4 col-md-3 col-lg-3" >
<div class="img-responsive"><p><a href="<?php echo $this->getCategoryUrl($_category)?>" class="product-image">
<?php
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
?>
<?
// If there is an image set for the category - Display it
if($_imgUrl=$this->getCurrentCategory()->getImageUrl()):?>
<img src="<?php echo $_imgUrl ?>" class="img-responsive" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
<?php endif; ?>
<?
// If there is not a image set for that Category - Display a random product Image
if(!$_imgUrl):
// Let's load the category Model and grab the product collection of that category
$product_collection = Mage::getModel('catalog/category')->load($_category->getId())->getProductCollection();
$product_collection->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);
// Now let's loop through the product collection and print the ID of every product
foreach($product_collection as $product) {
// Get the product ID
$product_id = $product->getId();
// Load the full product model based on the product ID
$full_product = Mage::getModel('catalog/product')->load($product_id);
// Now that we loaded the full product model, let's access all of it's data
// Let's get the Product Name
$product_name = $full_product->getName();
// Let's get the Product URL path
$product_url = $full_product->getProductUrl();
// Let's get the Product Image URL
$product_image_url = $full_product->getImageUrl();
// Let's print the product information we gathered and continue onto the next one
} //End For Each
?>
<img src="<?php echo $product_image_url; ?>" class="img-responsive" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />
<?php endif; ?>
</a>
</div>
<a href="<?php echo $this->getCategoryUrl($_category)?>">
<h2 class="text-center product-name" ><?php echo $_category->getName()?></a></h2>
<? if($_description=$this->getCurrentCategory()->getDescription()):?>
<p class="category-description">
<?php // echo $_description ?></p>
<?php endif ?>
</div>
<?php
endif;
endif;
endforeach;
?>
</div>
<br clear=all>
</div>
</div>
您在模板中定义了一个函数。函数只能定义一次
做对了™
不要使用函数。Magento的方法是定义扩展Mage_Catalog_block_Navigation
的自己的块,并将函数作为方法添加到此块中,或者将函数移动到帮助器类中
快速而肮脏的解决办法
将函数创建为闭包而不是普通全局函数:
$getImageUrl = function($category) {
// ...
}
这样称呼它:
<h2>Title</h2>
{{block type="catalog/navigation" category_id="5" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
<h2>Title</h2>
{{block type="catalog/navigation" category_id="9" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
<h2>Title</h2>
{{block type="catalog/navigation" category_id="38" template="/catalog/navigation/subcategorylisting-distinctive.phtml"}}
$getImageUrl($category)
我浏览了一下你的代码,它与你的问题无关,但我给你一个建议:不要在循环中使用load()
。迭代加载的类别集合,在循环中再次加载每个类别,在此循环中,对产品再次执行相同的操作。这是非常糟糕的表现。相反,请在集合上使用类似于addAttributeToSelect
的方法,以确保您需要的所有内容都已在第一次加载(这是一个大型数据库查询,而不是每个类别加一个查询),非常感谢!事实证明,我并不需要整个函数。