corcel / corcel

Use WordPress backend with Laravel or any PHP application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Get post category name

falahidev opened this issue · comments

Hi guys. How I can get post category name? I tried to find a way but I failed.

Hi there!

From a collection of categories

Well you can return a collection with all categories with the Taxonomy model.

Taxonomy::where('taxonomy', 'category')->get()

The collection contains all categories as models. You can call name in order to get the name of the category.

Taxonomy::where('taxonomy', 'category')->get()->first()->name

From a post

If you have a post and you need the category name then you will need to call category_name.

Post::published()->type('post')->slug($slug)->with('attachment')->firstOrFail()->category_name