- Posts: 6
COMMUNITY FORUM
- Forum
- K2 Community Forum
- English K2 Community
- Display current category name with K2 Tool module in article
Display current category name with K2 Tool module in article
- EMSA
- Topic Author
- Offline
- New Member
Less
More
4 years 4 months ago #176413
by EMSA
Display current category name with K2 Tool module in article was created by EMSA
Hi,
As you can understand from title i am trying to display category name in article using K2 Tool module.
This one didn't work
<?php echo $this->item->category->name; ?>
Than tried with query but also didn't work.
<?php
$db = JFactory::getDBO();
$articleid = JRequest::getInt('id');
$db->setQuery("SELECT alias FROM #__k2_categories WHERE id = {$item->catid}");
$category_title = $db->loadResult();
echo "$category_title";
?>
I hope somebody can help me.
Regards
Okan
As you can understand from title i am trying to display category name in article using K2 Tool module.
This one didn't work
<?php echo $this->item->category->name; ?>
Than tried with query but also didn't work.
<?php
$db = JFactory::getDBO();
$articleid = JRequest::getInt('id');
$db->setQuery("SELECT alias FROM #__k2_categories WHERE id = {$item->catid}");
$category_title = $db->loadResult();
echo "$category_title";
?>
I hope somebody can help me.
Regards
Okan
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 4 months ago #176416
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Display current category name with K2 Tool module in article
Directly calling the category in that module won't work as it's out of context.
The direct query option will work, but you need to update it to something like:
The direct query option will work, but you need to update it to something like:
<?php
$db = JFactory::getDBO();
$id = (int) JRequest::getInt('id');
$db->setQuery("SELECT c.name AS name, c.alias AS slug FROM #__k2_items AS i INNER JOIN #__k2_categories AS c ON i.catid = c.id WHERE i.id = {$id} AND i.published = 1");
$category = $db->loadResult();
echo "$category->name";
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- EMSA
- Topic Author
- Offline
- New Member
Less
More
- Posts: 6
4 years 4 months ago #176424
by EMSA
Replied by EMSA on topic Display current category name with K2 Tool module in article
Hello Fotis.
Thank you so much.
But unfortunately it doesn't display any result.
Thank you so much.
But unfortunately it doesn't display any result.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 4 months ago #176426
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Display current category name with K2 Tool module in article
Is PHP parsing enabled in the Custom Code settings of the K2 Tools module?
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- EMSA
- Topic Author
- Offline
- New Member
Less
More
- Posts: 6
4 years 4 months ago #176430
by EMSA
Replied by EMSA on topic Display current category name with K2 Tool module in article
You can see from attachment.
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 4 months ago - 4 years 4 months ago #176435
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Display current category name with K2 Tool module in article
Here's the right code:
The above also checks that you're within an "item" page.
<?php
$db = JFactory::getDBO();
$option = JRequest::getCmd('option');
$view = JRequest::getCmd('view');
$id = JRequest::getInt('id');
if ($option == 'com_k2' && $view == 'item') {
$db->setQuery("SELECT c.name AS name, c.alias AS slug FROM #__k2_items AS i INNER JOIN #__k2_categories AS c ON i.catid = c.id WHERE i.id = {$id} AND i.published = 1");
$category = $db->loadObject();
echo "$category->name";
}
The above also checks that you're within an "item" page.
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Last edit: 4 years 4 months ago by JoomlaWorks. Reason: Removed obsolete (int) casting
Please Log in or Create an account to join the conversation.
- EMSA
- Topic Author
- Offline
- New Member
Less
More
- Posts: 6
4 years 4 months ago #176448
by EMSA
Replied by EMSA on topic Display current category name with K2 Tool module in article
Worked like a charm.
Thank you so much.
Just in case if some one need it here is query for item name.
<?php
$db = JFactory::getDBO();
$articleid = JRequest::getInt('id');
$db->setQuery('SELECT title FROM #__k2_items WHERE id='.$articleid);
$myid=$db->loadResult();
echo $myid;
?>
Regards
Thank you so much.
Just in case if some one need it here is query for item name.
<?php
$db = JFactory::getDBO();
$articleid = JRequest::getInt('id');
$db->setQuery('SELECT title FROM #__k2_items WHERE id='.$articleid);
$myid=$db->loadResult();
echo $myid;
?>
Regards
Please Log in or Create an account to join the conversation.
- JoomlaWorks
- Offline
- Admin
Less
More
- Posts: 6218
4 years 4 months ago #176460
by JoomlaWorks
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Replied by JoomlaWorks on topic Display current category name with K2 Tool module in article
Great :)
Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Please Log in or Create an account to join the conversation.
- Forum
- K2 Community Forum
- English K2 Community
- Display current category name with K2 Tool module in article