Php 如何获取当前类别id-OpenCart

Php 如何获取当前类别id-OpenCart,php,opencart,Php,Opencart,我想添加到in header-if语句中: <?php if (category = 17) { ?> <meta name="description" content="category 17 description" /> <?php } ?> <?php if (category = 18) { ?> <meta name="description" content="category 18 description" /> &l

我想添加到in header-if语句中:

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

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


如何获取当前类别id

您想从哪里获取类别id

它在url中吗

然后必须使用全局$\u GET变量

如果url是

example.com/index.php?category=2

您可以使用

$category= $_GET["category"]

您想从哪里获取类别ID

它在url中吗

然后必须使用全局$\u GET变量

如果url是

example.com/index.php?category=2

您可以使用

$category= $_GET["category"]

把它放在标题中的代码之前

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

然后使用
$category
而不是像您在问题中那样只使用
category
,将其放在标题中的代码之前

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

然后使用
$category
而不是像您在问题中那样只使用
category
我想最好的方法是从
category
控制器中添加
元描述
,如下所示:

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

只需编辑您的
/catalog/controller/product/category.php
控制器文件。

我想最好的方法是从
类别
控制器中添加
元描述
,如下所示:

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

只需编辑您的
/catalog/controller/product/category.php
控制器文件。

如果您只需要类别id,它是一个url参数“path”。 因此,在header.tpl中访问category id的最佳方法是

<?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 } 
}  


如果只需要类别id,则它是url参数“path”。 因此,在header.tpl中访问category id的最佳方法是

<?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 } 
}  

资料来源:



来源:

你的分类ID在URL中传递了吗?在php中使用$before变量总是很好的,比如$VAR为什么你需要这样一个多余的变量?你会编辑每个类别的标题吗?您正在运行什么OC版本?编辑
/catalog/controller/product/category.php
并添加类似
$this->document->addMeta($category_info['description'))的内容不是更简单、更好的方法(或者可能是唯一正确的方法)吗???@shadyyx同意。这不是个好主意。@shadyyx谢谢,我选择了你的答案!您的类别ID在URL中传递了吗?在php中使用$before变量总是很好的,比如$VAR为什么需要这样一个多余的变量?你会编辑每个类别的标题吗?您正在运行什么OC版本?编辑
/catalog/controller/product/category.php
并添加类似
$this->document->addMeta($category_info['description'))的内容不是更简单、更好的方法(或者可能是唯一正确的方法)吗???@shadyyx同意。这不是个好主意。@shadyyx谢谢,我选择了你的答案!此外,在PHP中,在变量之前必须始终使用“$”。所以它应该是“$category”而不是“category”嗨,这是question@Maximii77你确定你说的是OpenCart吗?OpenCart为此使用
请求
,因此,与其直接访问
$\u GET
,不如使用
$this->request->GET
…在
GET
中也没有
类别
参数,但是
路径
参数…不会给你-1,因为你的XP很低…而且你在PHP中变量之前总是必须使用“$”参数。所以它应该是“$category”而不是“category”嗨,这是question@Maximii77你确定你说的是OpenCart吗?OpenCart使用
request
进行此操作,因此,您可以使用
$this->request->GET
,而不是直接访问
$\u GET
GET
中没有
类别
参数,但是
路径
参数…不会给您-1,因为您的XP较低…除非我弄错了,$this->document->addMeta($m)默认文档类中不存在。您可以使用这个vQmod来添加它,我现在不确定,因为答案已经有2年了,但很可能是一些旧版本(1.5.4.0之前)实现了这个方法,否则我无法在我的答案中使用它;-)我也在想,伙计,我想这个方法会一直存在,因为它在我们的语义时代非常有用。也许它在1.4.x分支或其他什么地方……或者……应该在2.x或其他地方:)除非我弄错了,$this->document->addMeta($m)在默认文档类中不存在。您可以使用这个vQmod来添加它,我现在不确定,因为答案已经有2年了,但很可能是一些旧版本(1.5.4.0之前)实现了这个方法,否则我无法在我的答案中使用它;-)我也在想,伙计,我想这个方法会一直存在,因为它在我们的语义时代非常有用。可能是在1.4.x分支或其他地方……或者……应该在2.x或其他地方:)