4

I want to get my current article's category and parent category link into my page. I get the category via the following code:

$db = JFactory::getDbo();
$id = $item->core_catid;
$db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.id='$id'");
$category = $db->loadResult();
echo '<p>'.$category.'</p>';

But I do not manage to get the parent category anyway. I try this but get no results:

$db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.parent_id='$id'");

4
  • This and this may help you
    – Lodder
    Commented May 15, 2014 at 11:28
  • This code is from my last answer and you can't use it onetoone for this task. Commented May 15, 2014 at 12:56
  • gag, in which file do you have this code now?
    – jonasfh
    Commented May 15, 2014 at 13:54
  • Yes, Joomla Agency, I just did not want to bother you so much). So I made another question for this. If you need it I can remove my question.
    – gag
    Commented May 15, 2014 at 17:25

3 Answers 3

5

Try this(tested and worked in Joomla 3.3.0):

Put this code:

<?php
$db = JFactory::getDbo();
$id = $item->core_catid;
$db->setQuery("SELECT cat.parent_id FROM #__categories cat WHERE cat.id='$id'");
$parent_category_id = $db->loadResult();
$db->setQuery("SELECT cat.title FROM #__categories cat WHERE cat.id='$parent_category_id'");
$parent_category_title = $db->loadResult();
if($parent_category_title != 'ROOT'){
 echo '<p>Parent Category: '.$parent_category_title.'</p>';
}
echo '<p>Parent Category: <a href="index.php?option=com_content&view=category&layout=blog&id='.$parent_category_id.'">'.$parent_category_title.'</a></p>';
?>

into this file:

templates/yourtemplate/html/com_tags/default_items.php

copy from:

components\com_tags\views\tags\tmpl\default_items.php

between this two lines

<?php foreach ($items as $i => $item) : ?> //Line 63
<?php endforeach; ?>
12
  • Ah, it's fine. It does what needed. And one more question (it's funny to ask you more questions). I want to make my category name to be links to their categories. Can I do it here?
    – gag
    Commented May 15, 2014 at 18:51
  • Can you accept my change for the title. Your title is misleading. This solution is only for the tag view... Commented May 15, 2014 at 18:56
  • i updated the answer. now with category link... Commented May 15, 2014 at 19:01
  • Yes I see, but the link leads to somewhere else with 404. I get this url mydomain.com/index.php/component/content/category/35-en/tutorials/art/drawing-and-painting?layout=blog
    – gag
    Commented May 15, 2014 at 19:05
  • Can you check the access level for the parent category? Commented May 16, 2014 at 14:27
2

Surely in your article options you can just set 'Show Category', 'Link Category, 'Show Parent' and 'Link Parent' to 'Show' (category) and 'Yes' (link).

1
  • No, it is not in the category, I made my tags result page look like category blog, I managed to do what I wanted except the parent category. I would like to have it also with a link to the parent category.
    – gag
    Commented May 15, 2014 at 17:23
1

I think you could do this using the Content model to load the article.

/* Lets say the article ID is stored in $id */

/* Get an instance of the generic articles model */
$model = JModelLegacy::getInstance('Articles', 'ContentModel');
$myArticle = $model->getItem($id);

$categoryID = $myArticle->catid; /* catid = "9" */
$category_title = $myArticle->category_title; /* category_title = "Blog" */
$category_alias = $myArticle->category_alias; /* category_alias = "blog" */

$parent_id = $myArticle->parent_id; /* parent_id = "1" */
$parent_title = $myArticle->parent_title; /* parent_title = "ROOT" */
$parent_alias = $myArticle->parent_alias; /* parent_alias = "root" */

The values shown in the right-hand side comments are what I just pulled out of a demo install of Joomla 3.3 with the "Blog" sample content installed. Note the value of the root category.

1
  • I do not know whether I place the code correctly, but as I paste it in the same place where my old code with database call was, my site breaks down.
    – gag
    Commented May 15, 2014 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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