![](/img/trans.png)
[英]How can i get all post of taxonomy category post by taxonomy category id or taxonomy category name in wordPress?
[英]CakePHP: How can I select all elements of one category with a “nice” url? (/category/NAME_OF_CATEGORY)
现在我有两张桌子。 类别和网站。 我有一个名为Cars(id:5)的类别和属于该类别的两个网站(bmw.com,toyota.com)。 如果我现在想要查看所有汽车的网站,我使用普通的url / categories / view / 5。 但我希望有一个像/ categories / cars /这样的网址来查看同一页面。 所以基本上不是动作而是我需要在类别表中使用行的名称。
你能帮助我实现这个目标吗? 谢谢干杯
这很简单 - 只需阅读有关路由的非常深入的文档,您就可以在几分钟内解决它:
为了实现您的要求,您需要对两件事做出更改:
app/Config/route.php
文件。 app/Controller/CategoriesController.php
文件。 在app/Config/route.php
:
Router::connect('/categories/:name', array(
'controller' => 'categories', 'action' => 'viewByName'
),
array(
'pass' => 'name',
'name' => '[a-zA-Z0-9]+'
)
);
在app/Controller/CategoriesController.php
:
public function viewByName($name = null) {
$option = array(
'conditions' => array('Category.name'=>$name)
);
$category = $this->Category->find('first', $options);
$this->set('category', $category);
}
然后,您可以将app/View/Categories/view.ctp
的内容复制到viewByName.ctp
并viewByName.ctp
润色一下。
我给你最低限度,所以请出于安全原因添加检查变量内容等等。
您还可以通过查看Cake Documentation上的Routing页面了解更多信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.