2

I have tried to search on every Joomla site to find a solution but haven't found a proper result yet.

I have created this custom module for my page and I need to display the "corrent" category title. I know I figured out that the original joomla com_content shows the corrent category by using this code:

defined('JPATH_BASE') or die;

?>
<div class="category-name"> 

    <?php $title = $this->escape($displayData['item']->category_title); ?>
    <?php if ($displayData['params']->get('link_category') && $displayData['item']->catslug) : ?>
        <?php echo '<a href="' . JRoute::_(ContentHelperRoute::getCategoryRoute($displayData['item']->catslug)) . '" itemprop="genre" data-toggle="tooltip"><i class="fa fa-folder-open-o"></i> ' . $title . '</a>'; ?>
    <?php else : ?>
        <?php echo '<span itemprop="genre" itemprop="genre" data-toggle="tooltip" title="' . JText::_('COM_CONTENT_CONTENT_TYPE_CATEGORY') . '">' . $title . '</span>'; ?>
    <?php endif; ?>
</div>

I tried to copy/paste it to my module, but it didn't work. I figured out I need to first import Joomla component helper. something like this:

jimport('joomla.application.component.helper');
$content_params = JComponentHelper::getParams( 'com_content' );
$show_category = $content_params->get( 'show_category' );

and now I need to display the category title, but I dont know how to do that, can anyone please help me?

2

Here is a quick bit of code that will retrieve the category name for you. This code assumes that you are on an article page.

<?php

$JInput = JFactory::getApplication()->input;
$articleId = $JInput->get('id','','int');

$categoryName = getCategoryName($articleId);

echo $categoryName;

function getCategoryName($articleId)
{
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->select('c.title');
    $query->from('#__categories AS c');
    $query->join("INNER","#__content AS a ON c.id = a.catid");
    $query->where("a.id = '$articleId'");
    $db->setQuery($query);
    $row = $db->loadObject();

    return $row->title;
}
?>

For a single artilce menu item the URL generated in the database contains an id parameter. JInput grabs the value of id and the queries the database using the getCategoryName function which returns the correct category name for you as the variable $categoryName.

Have a good one.

  • wow thank you very much. i appreciate it. exactly what i needed. thank you sir! – Peter Feb 4 '16 at 22:15
  • No Problem Peter, glad I could help. – Terry Carter Feb 5 '16 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.