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:
<?php $page_object = get_queried_object(); ?>
<h1><?php echo $page_object->cat_name; ?></h1>
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) ); get url
$url_array = explode('/',$current_url);
$retVal = !empty($url_array[5]) ? $url_array[5] : $url_array[4] ;
$idObj = get_category_by_slug($retVal);
echo $idObj->name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With