By Adam O. on Tuesday, 29 December 2015
Replies 12
Likes 0
Views 1.1K
Votes 0
Hi there,

I've got a question about categories.

I need to check in list of posts what is main category, where the post is added.

My categories structure looks like below:

Main category 01
- subcategory 01
- subcategory 02

Main category 02
- subcategory 03
- subcategory 04

and I need to show not directly category, where the post is added, but category above subcategory.

So how can I check this and show in post in list view (default.main.php) by adding some short code?

I'll be very grateful for your help with this thread!
Best regards!
Adam
Hello Adam,

You can use this code in default.main.php:

if ($post->category->parent_id) {
$mainCat = EB::table('category'); // get the table object
$mainCat->load($post->category->parent_id); // load the category
var_dump($mainCat);exit;
}


Hope this helps.
·
Tuesday, 29 December 2015 16:40
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi Nik!

Thank you for your help!

I put your code into default.main.php

and add:

echo $mainCat;


but doesn't work - it loads me nothing...

Please provide me what I do wrong

Best regards!
Adam
·
Wednesday, 30 December 2015 20:17
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Adam,

The mainCat is actually a category object. You might want to use echo $mainCat->title;
·
Wednesday, 30 December 2015 23:12
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Adam,

The mainCat is actually a category object. You might want to use echo $mainCat->title;
·
Wednesday, 30 December 2015 23:12
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi Nik,

thank you for your reply.

But $mainCat->title;
still loads me nothing...

How can I load main category (not subcategory)?
·
Monday, 04 January 2016 16:53
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Adam,

Correct me if I'm wrong, did want to load the parent category of the category that the post belongs to? Can you provide us your backend, FTP access and the file that you modifying?

By the way, this is actually beyond our support scope. I will help you only for this time.
·
Monday, 04 January 2016 19:11
·
0 Likes
·
0 Votes
·
0 Comments
·
Nik,

very thank you for your help

I shared access to my ftp and site in first post (I edited it)

Just note, that after /administartor in backend acces you need to add key (I shared it too)

best regards!
Adam
·
Tuesday, 05 January 2016 07:03
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Adam,

I can't access your backend with the key provided. However, I've checked the your default.main.php, and found out that your post category doesn't have parent category. : http://screencast.com/t/8ADzdsSrnD6P . I've added this code to check the parent_id of the category: http://screencast.com/t/8jBGH9Uout1Y
·
Tuesday, 05 January 2016 10:26
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi Nik,

I have a kindly please for you - could you please remind me how to load primary category title and ID?

As I remember, it was something very simple, but I forget this line of code...

I'll be very grateful for your help!
Best regards!
Adam
·
Sunday, 06 November 2022 10:26
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Adam,

Do you mean the primary category for specific post? You can try the code below:


$primaryCategory = $post->getPrimaryCategory();

echo $primaryCategory->getTitle();


Hope this helps.
·
Monday, 07 November 2022 08:52
·
0 Likes
·
0 Votes
·
0 Comments
·
yes :-)

many thanks!

best regards!
·
Tuesday, 08 November 2022 02:00
·
0 Likes
·
0 Votes
·
0 Comments
·
You're welcome.
·
Tuesday, 08 November 2022 10:19
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post