php 如何获取当前的类别 ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5506965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:39:58  来源:igfitidea点击:

How do I get the current catageory id?

phpxmlvariableslayoutmagento

提问by Chris

I have a CMS page that I am going to display products on with the following updated XML code:

我有一个 CMS 页面,我将使用以下更新的 XML 代码在该页面上显示产品:

<reference name="content">
    <block type="catalog/product_list"  name="product_list" template="catalog/product/wholesale-list.phtml">
        <action method="setCategoryId"><category_id>191</category_id></action>
        <action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
    </block>
</reference> 

I have tried getting the ID I set in the layout, but no such luck. I have tried:

我尝试获取我在布局中设置的 ID,但没有这样的运气。我试过了:

$_category = Mage::registry(‘current_category');
$currentCategoryId= $_category->getId();

and

$layer = Mage::getSingleton(‘catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();

But neither of these methods work. Does anyone know how I can get the ID?

但是这两种方法都不起作用。有谁知道怎么办身?

采纳答案by Jonathan Day

Haven't tried this, but maybe something like:

没试过这个,但可能是这样的:

$this->getLayout()->getBlock('product_list')->getCategoryId()

This way you are directly getting the variable that you have set on the Block object in the XML.

通过这种方式,您可以直接获取在 XML 中的 Block 对象上设置的变量。

Cheers,
JD

干杯,
JD

回答by Ansyori

i think this is the best way ;)

我认为这是最好的方法;)

Mage::registry('current_category')->getId();

回答by Rohit Goel

Try below code

试试下面的代码

 Mage::getModel('catalog/layer')->getCurrentCategory()->getId();

回答by Meetai.com

This works for me:

这对我有用:

$layer = Mage::getSingleton('catalog/layer');
$_category = $layer->getCurrentCategory();
$currentCategoryId= $_category->getId();

回答by shaune

Have you considered updating the layout of the catalog page rather than making a CMS page? I suppose there are situations where you might prefer a CMS page, but you can update the layout of a category fairly easily, well about as easy as it gets in Magento, which isn't all that easy :)

您是否考虑过更新目录页面的布局而不是制作 CMS 页面?我想在某些情况下,您可能更喜欢 CMS 页面,但是您可以相当轻松地更新类别的布局,就像在 Magento 中一样简单,但这并不是那么容易:)

Login to the admin backend, go to Catalog -> Manage Categories, then choose the category you want, then click the Custom Design tab. Notice the Custom Layout Updates field. This is where you can put your layout updates.

登录后台后台,进入目录 -> 管理类别,然后选择你想要的类别,然后点击自定义设计选项卡。请注意自定义布局更新字段。这是您可以放置​​布局更新的地方。

So for this category, if you didn't want to display a specific block you could do something like

所以对于这个类别,如果你不想显示一个特定的块,你可以做类似的事情

<reference name="right">
        <remove name="right.permanent.callout" />
</reference>

Which would remove the block named right.permanent.callout from the layout altogether. And if you wanted to just change the product listing to use your specific phtml file you could do something like...

这将从布局中完全删除名为 right.permanent.callout 的块。如果您只想更改产品列表以使用您的特定 phtml 文件,您可以执行以下操作...

<reference name="product_list">
        <action method="setTemplate"><template>catalog/product/wholesale.phtml</template></action>
</reference>

You can probably use google to find out more about how to layouts.

您可能可以使用 google 查找有关如何布局的更多信息。

回答by codiga

This worked for me:

这对我有用:

$currentCat = $this->getLayout()->getBlock('category.products')->getCurrentCategory();

Then you have the current category as an object and you can get the id by:

然后您将当前类别作为对象,您可以通过以下方式获取 id:

$currentCat->getId();