How to get current category id - OpenCart

£可爱£侵袭症+ 提交于 2020-01-03 01:36:04

问题


I want to add to in header - if statement:

<?php if (category = 17) { ?>
<meta name="description" content="category 17 description" />
<?php } ?>

<?php if (category = 18) { ?>
<meta name="description" content="category 18 description" />
<?php } ?>

How can i get current category id


回答1:


Put this before the code in your header

$category = empty($this->request->get['path']) ? 0 : (int) array_pop(explode('_', $this->request->get['path']));

Then use $category instead of just category as you have in your question




回答2:


If you only require the category id , it is a url parameter "path". So, the best way to access category id in the header.tpl will be

<?php if(isset($_GET['category_id'])){
  if ($_GET['path'] = 17) { ?>
<meta name="description" content="category 17 description" />
<?php } ?>

<?php if ($_GET['path'] = 18) { ?>
 <meta name="description" content="category 18 description" />
<?php } 
}  



回答3:


Where do you want to get the Category ID from?

Is it in the url?

Then you have to use the global $_GET variable.

if the url is

example.com/index.php?category=2

You can get the parameter with

$category= $_GET["category"]



回答4:


I guess best approach to do this is to add meta description from within the category controller, like this way:

$this->document->addMeta($category_info['description']);

Just edit Your /catalog/controller/product/category.php controller file.




回答5:


if(isset($this->request->get['path'])) {
    $path = $this->request->get['path'];
    $cats = explode('_', $path);
    $cat_id = $cats[count($cats) - 1];
}
echo $cat_id;

Source: http://chandreshrana.blogspot.in/2014/07/get-current-category-in-opencart.html



来源:https://stackoverflow.com/questions/14683090/how-to-get-current-category-id-opencart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!