Keyword

Display parent category url on item.php

  • Nathan
  • Nathan's Avatar Topic Author
  • Offline
  • New Member
More
2 months 2 weeks ago - 2 months 2 weeks ago #173039 by Nathan
Nathan created the topic: Display parent category url on item.php
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: 2 months 2 weeks ago by Nathan.

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
2 months 2 weeks ago - 2 months 2 weeks ago #173049 by Fotis
Fotis replied the 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.

If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member
Last edit: 2 months 2 weeks ago by Fotis.

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

  • Nathan
  • Nathan's Avatar Topic Author
  • Offline
  • New Member
More
2 months 2 weeks ago - 2 months 2 weeks ago #173052 by Nathan
Nathan replied the 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: 2 months 2 weeks ago by Nathan.

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

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
2 months 2 weeks ago #173058 by Fotis
Fotis replied the 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))));

If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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

  • Nathan
  • Nathan's Avatar Topic Author
  • Offline
  • New Member
More
2 months 2 weeks ago #173074 by Nathan
Nathan replied the 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.

  • Fotis
  • Fotis's Avatar
  • Offline
  • Administrator
  • JoomlaWorks Support Team
More
2 months 2 weeks ago #173078 by Fotis
Fotis replied the topic: Display parent category url on item.php
Great :)

If you use & love K2, please take a moment to add your review and rate it
at the Joomla Extensions Directory: extensions.joomla.org/extension/k2/


IMPORTANT: Please search the forum before posting a question!

JoomlaWorks Support Team Member

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


Powered by Kunena Forum