Keyword

Display parent category url on item.php

  • Nathan
  • Nathan's Avatar Topic Author
  • Offline
  • Junior Member
More
4 years 7 months ago - 4 years 7 months ago #173039 by Nathan
I'm building a magazine style website and have a need to display an Item's parent category and url on the item.php template.

The magazine is organised by Issue and then sub-categories for different sections of the magazine - eg. Art, History - but ideally we'd like to have a link on each article page going to the main contents page... so in the K2 structure, the parent category.

I found this very old post which I've been able to use to retrieve the parent category name - www.joomlaworks.net/forum/k2-en/28957-solved-show-parentcategory-in-category-item-php - but wondering if anyone has any suggestions as to how I might retrieve it's URL as well?

Do I need to run some DB queries directly in the template file?

Thanks

Nathan
Last edit: 4 years 7 months ago by Nathan.

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

More
4 years 7 months ago - 4 years 7 months ago #173049 by JoomlaWorks
Replied by JoomlaWorks on topic Display parent category url on item.php
One way is indeed what is referenced in that other thread. This is the best way.
<?php

$parent = JTable::getInstance('K2Category', 'Table');
$parent->load($this->item->category->parent);

echo $parent->name;

The code above is "portable", meaning it can be used anywhere in K2 templates.

The other is to use the breadcrumbs option in K2 Tools and hide all children categories (but the parent) using CSS.

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)
Last edit: 4 years 7 months ago by JoomlaWorks.

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

  • Nathan
  • Nathan's Avatar Topic Author
  • Offline
  • Junior Member
More
4 years 7 months ago - 4 years 7 months ago #173052 by Nathan
Replied by Nathan on topic Display parent category url on item.php
Thanks for the quick reply Fotis.

Just on the method referenced in the other thread... that's only giving me the unlinked "Name" of the parent category, not it's URL as well

Is there a similar method I can use to get a linked version of the parent category?

I had a look at the "TableK2Category" class and I couldn't see any way from that of pulling in it's url... has alias, description etc, but not url that I can see.

Regardless I think I'll be able to get by with the breadcrumb, which was a workaround I'd considered, but it would be much easier if I can use something like the above method.

Thanks

Nathan
Last edit: 4 years 7 months ago by Nathan.

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

More
4 years 7 months ago #173058 by JoomlaWorks
Replied by JoomlaWorks on topic Display parent category url on item.php
If you do a var_dump on $parent, you'll see all available variables to use. I assume you're able to see $parent->id and $parent->alias, in which case, constructing the URL would be a matter of calling:
$parent = JTable::getInstance('K2Category', 'Table');
$parent->load($this->item->category->parent);

$parentCategoryURL = urldecode(JRoute::_(K2HelperRoute::getCategoryRoute($parent->id.':'.urlencode($parent->alias))));

Fotis / JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

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

  • Nathan
  • Nathan's Avatar Topic Author
  • Offline
  • Junior Member
More
4 years 7 months ago #173074 by Nathan
Replied by Nathan on topic Display parent category url on item.php
Thanks very much Fotis, that's what I was after

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

More
4 years 7 months ago #173078 by JoomlaWorks
Replied by JoomlaWorks on topic Display parent category url on item.php
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