Keyword

Display current category name with K2 Tool module in article

More
3 years 9 months ago #176413 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

Please Log in or Create an account to join the conversation.

More
3 years 9 months ago #176416 by JoomlaWorks
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:
<?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.

More
3 years 9 months ago #176424 by EMSA
Hello Fotis.
Thank you so much.
But unfortunately it doesn't display any result.

Please Log in or Create an account to join the conversation.

More
3 years 9 months ago #176426 by JoomlaWorks
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.

More
3 years 9 months ago #176430 by EMSA
You can see from attachment.
Attachments:

Please Log in or Create an account to join the conversation.

More
3 years 9 months ago - 3 years 9 months ago #176435 by JoomlaWorks
Here's the right code:
<?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: 3 years 9 months ago by JoomlaWorks. Reason: Removed obsolete (int) casting

Please Log in or Create an account to join the conversation.

More
3 years 9 months ago #176448 by EMSA
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

Please Log in or Create an account to join the conversation.

More
3 years 9 months ago #176460 by JoomlaWorks
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.


Powered by Kunena Forum