开发者

Get Joomla! category from an URL

开发者 https://www.devze.com 2023-01-16 03:44 出处:网络
How can I read the section a certain URI belongs to? I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically hold

How can I read the section a certain URI belongs to?

I want to enhance the mod_breadcrumb to put section and category into the HTML. JApplication->getPathway() returns a JPathway which basically holds an assiciative array combining a name and an URL (as $list[]->name and $list[]->link). I think, it 开发者_运维问答should be possible to get the section and category from a link, but don't know how.

A starting point could be the parsing into JURI-Object, but from there I don't know how get get further. Any ideas?


Pretty straight forward...

I assume you want to add category and section for the article and not your custom component.

Check if requested current URL is for article. If it is for article you know the article ID, use this article Id to go database and get catid from #__content, Use this cat_id to go to #__categories and get section (this is section id), go to #__sections to get the proper section name. All this can be done in 1 sql statement.

$breadcrumbs =& JFactory::getApplication()->getPathway();
$breadcrumbs->addItem("SECTION_NAME", JRoute::_("index.php?option=com_content&view=section&id=SECTION_ID"));
$breadcrumbs->addItem("CATEGOY_NAME", JRoute::_("index.php?option=com_content&view=category&id=CATEGORY_ID"));
$breadcrumbs->addItem("Article");

Alternatively, if you know the URL from the breadcrumb item. You can parse it and get IDS. The trick here is not to get the default URI object by JFactory::getURI() because things will get ugly, use JFactory::getURI('YOU_URI_NAME').

<?php
//  You need to get Your own uri, you do not want to modify default URI
//  because this will messup a lot of things
$uri = JFactory::getURI('MyCustomURI');

//  Test # 1 [ID = SECTION_ID]
$url = "index.php?option=com_content&view=section&id=SECTION_ID";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');

//  Test # 2 [ID = 123]
$url = "index.php?option=com_content&view=section&id=123";
$uri->parse($url);
echo "CURRENT SECTION = " . (int) $uri->getVar('id');
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号