Keyword

Show name of parent category in itemlist

  • Peter Haupt
  • Peter Haupt's Avatar Topic Author
  • Offline
  • New Member
More
15 years 5 months ago #76045 by Peter Haupt
Show name of parent category in itemlist was created by Peter Haupt
I'm trying to figure out how to display the name of the parent category in my views (itemlist, item).

I suppose I'll need to add some code somewhere comparing the "catid" out of the table "jos_k2_item" with the "id" out of the table "jos_k2_categories" and then I have to compare the "parent"-value with the "id" in the table "jos_k2_categories" to get the "name".
Here are my questions in which file would I have to insert the code and what would be the best way to solve this problem!

Thx

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

  • Peter Haupt
  • Peter Haupt's Avatar Topic Author
  • Offline
  • New Member
More
15 years 5 months ago #76046 by Peter Haupt
Replied by Peter Haupt on topic Show name of parent category in itemlist
Still trying to solve this problem. Can anybody help me, please?

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

  • Peter Haupt
  • Peter Haupt's Avatar Topic Author
  • Offline
  • New Member
More
15 years 5 months ago #76047 by Peter Haupt
Replied by Peter Haupt on topic Show name of parent category in itemlist
I'm trying to solve this problem with a code-snippet in generic.php:








Unfortunately it's not working they way I want it to. I know that isn't the best way to solve the problem. Can anybody help or give my an advice to solve this problem in a better way!

Thanks.

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

More
15 years 5 months ago #76048 by bkemler
Replied by bkemler on topic Show name of parent category in itemlist
generic.php only controls the tag view and the user view. This post about creating custom templates in k2 may be helpful... k2community.joomlaworks.gr/notes/Templating_in_K2

Peter Haupt said:I'm trying to solve this problem with a code-snippet in generic.php:







Unfortunately it's not working they way I want it to. I know that isn't the best way to solve the problem. Can anybody help or give my an advice to solve this problem in a better way!

Thanks.

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

More
14 years 7 months ago #76049 by Daniel Arnolf
Replied by Daniel Arnolf on topic Show name of parent category in itemlist
Please, anyone with any ideas, using the parent category name or id can save developing many subcategories overrides, at least in my case. I know the works around with inheritance etc, I just need a way to retrieve that parameter. I have png main category titles and I want to use a switch-case to change those titles and other stuff according to the parent category ID or name.
Thanks

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

More
14 years 6 months ago #76050 by Steinar A
Replied by Steinar A on topic Show name of parent category in itemlist
Hi all.

I'm fetching the name of the parent category for a sub-category by doing this in the view.html.php file:
$parentcategory = & JTable::getInstance('K2Category', 'Table');
$parentcategory->load($item->category->parent); //$item->category->parent is the id of the current (sub-)category
print_r($parentcategory->name);

Hope it helps someone :)

Regards,
Steinar

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

More
14 years 6 months ago #76051 by Daniel Arnolf
Replied by Daniel Arnolf on topic Show name of parent category in itemlist
Hi ,thank you very much, can you please be a bit more specific? I'm including it in /components/com_k2/views/item/view.html.php , below "$category->load($item->catid);" line ~575, then I try to output this variable in my template override item.php, with echo $this->item->parentcategory->name and other options, but not getting anywhere, I'll appreciate your help.

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

More
14 years 6 months ago #76052 by Steinar A
Replied by Steinar A on topic Show name of parent category in itemlist
Hi Daniel.
I just need to display it for SEO-reasons, so I use it on line ~240 like this:
$parentcategory = & JTable::getInstance('K2Category', 'Table');
$parentcategory->load($item->category->parent);
$document->setTitle($parentcategory->name . " " . $item->category->name . " " . $params->get('page_title'));

Regards,
Steinar


Daniel Arnolf said:Hi ,thank you very much, can you please be a bit more specific? I'm including it in /components/com_k2/views/item/view.html.php , below "$category->load($item->catid);" line ~575, then I try to output this variable in my template override item.php, with echo $this->item->parentcategory->name and other options, but not getting anywhere, I'll appreciate your help.

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

More
14 years 6 months ago #76053 by Daniel Arnolf
Replied by Daniel Arnolf on topic Show name of parent category in itemlist
Hi Steinar
I implemented your snippet successfully, to later understand that I can output the ID of the parent category without touching the core files: "echo $this->item->category->parent" , which is closer to what I was looking for running a switch and print graphic title according to parent category. Thank you very much indeed.

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

More
14 years 5 months ago #76054 by mchmaster
Replied by mchmaster on topic Show name of parent category in itemlist
Hi Daniel and Steiner,

 

I've been trying to do this exact thing except in my item listing in a k2_content module on my frontpage.

I tried using "echo $this->item->category->parent" as you said it worked for you and also "echo $item->category->parent". Obviously, since I'm posting here I wasn't able to get it to work.

 

Is there a way to get your method to work in a k2_content module displaying a list of recent articles?

I would really appreciate your help. Thanks.

 

Daniel Arnolf said:

Hi Steinar I implemented your snippet successfully, to later understand that I can output the ID of the parent category without touching the core files: "echo $this->item->category->parent" , which is closer to what I was looking for running a switch and print graphic title according to parent category. Thank you very much indeed.

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

More
14 years 5 months ago #76055 by Steinar A
Replied by Steinar A on topic Show name of parent category in itemlist
Seems like the $item in the K2 module doesn't contain the parent-attribute.

This is not a nice hack, but you can "reload" the category by:

(in modules/mod_k2_content/tmpl/Default/default.php (or make your own template)).

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

Then you can access the parent-attribute by:

echo $category->parent;

 

And if you also need the name of the parent category:

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

echo $parentcategory->name;

 

I have no idea how to make K2 content displaying a list of recent articles tho...

 

Steinar

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

More
14 years 5 months ago #76056 by mchmaster
Replied by mchmaster on topic Show name of parent category in itemlist
Thank you so much for your quick reply. I tried to apply those snippets to my module template, but all I got was a white screen with the first article title and nothing else, no error report either. Could this have to do with the fact that I'm calling multiple articles at once instead of just viewing a single article?

 

My hope is to create a recent article listing with articles from different categories. Each article's parent category will be shown next to the article title. If I can call each parent category, then I can make individual styles as well.

 

If you go to www.blessedteensacademy.com and look at the module in the middle titled "New Content", you'll see where I'm trying to apply this. 

 

Any ideas? Thank you so much for your time! Steinar A said:

Seems like the $item in the K2 module doesn't contain the parent-attribute.

This is not a nice hack, but you can "reload" the category by:

(in modules/mod_k2_content/tmpl/Default/default.php (or make your own template)).

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

Then you can access the parent-attribute by:

echo $category->parent;

 

And if you also need the name of the parent category:

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

echo $parentcategory->name;

 

I have no idea how to make K2 content displaying a list of recent articles tho...

 

Steinar

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

More
14 years 3 months ago #76057 by mchmaster
Replied by mchmaster on topic Show name of parent category in itemlist
If anyone can help with this, I'm stuck!!!!

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

More
13 years 11 months ago #76058 by Duane Arnett
Replied by Duane Arnett on topic Show name of parent category in itemlist
Is there a way to get the category parent in index.php?  

I dont see anyway of doing this?

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

More
13 years 9 months ago #76059 by petzov
Replied by petzov on topic Show name of parent category in itemlist
Hi guys,

This worked for me. Put this in index.php.

$database = &JFactory::getDBO();            

$itemid= JRequest::getVar('id',0);                          

$sql = "SELECT name FROM jos_k2_items t1, jos_k2_categories t2 WHERE t1.catid = t2.id and t1.id='".$itemid."'" ;                                         $database->setQuery( $sql );                                         $row=$database->loadResult();                                         echo $row;

 

It is an old post, but somebody may need it in the future.

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


Powered by Kunena Forum