php – How to get the last category name of a child category?
there. I’ve tried tons of solutions I’ve found in this forum before asking this. It’s kind of hard for me to understand the php sintaxe. I have no idea how to do this and i’ve been stuck for days trying hundreds of codes. Can someone help me?
I have the following categories:
- Animal
-- Dog
--- frenchie-1
--- frenchie-2
--- frenchie-3
Animal > Dog > frenchie-3
I need to get the slug from “frenchie-3”, that is the last one in this situation. Maybe in the future will be a frenchie-4 and I would need to retrieve this “new last” category of that child category. I will always need the last one.
Thanks a lot for your attention.
Answer ( 1 )
I’ve finally figured out the result I wanted with get_categories:
get_categories( array( 'orderby' => 'name', // get the name of category 'order' => 'DESC', // descending order 'parent' => 14, // category_id 'number' => 1 // only one category (the last one, because DESC) ));