I have hard time trying to find the solution, does somebody know how to get this:
I have one WordPress post in more than one category, but only one is permalink category. I need to get the ID only of that permalink category (I need this info so I can take few latest posts from permalink category via custom query).
url looks like this http://domain.com/category-name/post-title
I need that “category-name” ID.
A Good one to use is:
My answer is:
If the post belongs to many categories, what if you’re viewing the post from a second category. In that case retrieving the category ID of the permalink category may not help, since you would need the related posts of the current category in action.
For that, you can get the ID by passing the category name as follows:
Does this help?
WordPress chooses the oldest category as the permalink category. There’s no way to change that behavior unless you use some plugin. If you choose to use a plugin you make take the category ID from plugin settings.
You can list all categories of this post and choose most relevant category.
Use the following code inside The Loop: