WordPress Get value of queried category in URL

is it possible to know what’s the first category queried in a wordpress URL?

Example:

Read More

http://www.mywebsite.com/category/mycat/mysubcat/

My Categories hierarchy is Something Like

Parent Cat 1
Parent Cat 2
Parent Cat 3
  Subcat 1
  Subcat 2
  Subcat 3
Parent Cat 4
  Subcat 1
  Subcat 2
  Subcat 3

What i’m trying to do is to echo the “mycat” value in archive.php, to do a conditional similar to:

if($value ==”mycat”) {

}
else { … }

Why it’s a little bit complicated? because each post belongs to at least 1 sub-cat from each parent plus 1 of the parent category with no childs.

I need something similar to $cat = get_query_var(‘cat’); (in this case $cat returns mysubcat)

Any Ideas?

Thanks so much!!

Related posts

Leave a Reply

2 comments

  1. The easiest way to accomplish this is:

    $uri = $_SERVER['REQUEST_URI'];
    $elms = explode('/', $uri) ;
    $firstcat = $elms[2] ;
    
    if($firstcat == "products") {
    
    // We show products grid
    
    }
    
    elseif($firstcat == "accesories") {
    
    // We Show accesories grid
    
    }
    
    else {
    
    // Whatever!
    
    }