1

I'm building a module and using the Category Form Field Type, as outlined in the Joomla docs here https://docs.joomla.org/Category_form_field_type

In the module XML I've used the following:

<field name="catOne" type="category" extension="com_content" label="Select a category" description="" />
<field name="catTwo" type="category" extension="com_content" label="Select a category" description="" />
<field name="catThree" type="category" extension="com_content" label="Select a category" description="" />
<field name="catFour" type="category" extension="com_content" label="Select a category" description="" />

Whenever I echo these in the frontend, I'm just getting the category ID - how can I echo the category title and it's related URL for each of the above variables?

1

Yo have the ID of the category, so you can perform a database query to get the title, like so:

Get the title from the ID:

// Get the category parameter value 
// Define a default value of 0
$cat_id = $params->get('catOne', 0);

// Get instance of database connection
$db = JFactory::getDbo();

// Query the #__categories table in the database
$query = $db->getQuery(true);
$query->select($db->quoteName('title'))
      ->from($db->quoteName('#__categories'))
      ->where($db->quoteName('id') . ' = '. (int)$cat_id);
$db->setQuery($query);

// Try and get the result
try
{
    $result = $db->loadResult();
}
catch (Exception $e)
{
    // If there is no result, make the variable empty
    $result = '';
}

// Display the result
echo $result;

Display a Link:

To display the URL for the category, you will need to import com_content's router:

require_once JPATH_SITE . '/components/com_content/helpers/route.php';

$link = JRoute::_('index.php?option=com_content&view=category&layout=blog&id=' . $cat_id);

echo '<a href="' . $link . '">' . $result . '</a>';

For multiple params, use a function:

function displayCategoryLink($id)
{
    require_once JPATH_SITE . '/components/com_content/helpers/route.php';

    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $query->select($db->quoteName('title'))
          ->from($db->quoteName('#__categories'))
          ->where($db->quoteName('id') . ' = '. (int)$id);
    $db->setQuery($query);

    try
    {
        $result = $db->loadResult();
    }
    catch (Exception $e)
    {
        $result = '';
    }

    $link = JRoute::_('index.php?option=com_content&view=category&layout=blog&id=' . $id);   
    $html = '<a href="' . $link . '">' . $result . '</a>';

    return $html;
}


displayCategoryLink($params->get('catOne', 0));
displayCategoryLink($params->get('catTwo', 0));
displayCategoryLink($params->get('catThree', 0));
displayCategoryLink($params->get('catFour', 0));

The function above is just basic. I would of course suggest you put the function in a class, in a helper.php file and call it properly

Hope this helps

  • Thanks Lodder - how can I then echo the URL for this category? – RustyJoomla Sep 29 '15 at 15:20
  • If I have mroe than one category variable, how will this work also? I've updated the question above – RustyJoomla Sep 29 '15 at 15:25
  • @RustyJoomla - More than welcome ;) – Lodder Sep 29 '15 at 16:20
  • One last question, how can I seperate the $link and the $result - so for example if I want to add a href onto a button but have the category name as text within the button? – RustyJoomla Sep 29 '15 at 16:52
  • @RustyJoomla The way I've written the function for you in the last code snippet, this outputs a clickable link with the Category Title as the actual link, e.g: Some Title. Maybe I mis-read your comment if you're referring to something different – Lodder Sep 29 '15 at 17:06

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.