Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress get category ID from URL

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.

like image 719
Danielp Avatar asked Feb 19 '23 22:02

Danielp


2 Answers

A Good one to use is:

<?php $page_object = get_queried_object(); ?>
<h1><?php echo $page_object->cat_name; ?></h1>
like image 147
Rees McIvor Avatar answered Feb 26 '23 22:02

Rees McIvor


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
like image 34
Irlando Pereira Avatar answered Feb 26 '23 20:02

Irlando Pereira