views:

323

answers:

3

I was wondering which is the best approach to get the catgeory ID when listing the posts within a particular category. Normally, the urls look something like this : www.example.com/?cat=4 and it is pretty easy to get the id. However, I really need the urls to be routed like this www.example.com/categories/hotels . wordpress provides an easy way to do the "pretty" routing, however all of the GET paramater information is lost this way. In this case, the $_GET variable is assigned nothing. I need to be able to say $category = $_GET["cat"] or something like that

What is the easiest approach ?

A: 

What about the category base setting in the backend? "Configuration" > "Permalinks" and there the last paragraph. See here for docs.

Boldewyn
how would you call that in archives.php/categories.php . Please, give a specific example
Do I get it right, that you are speaking of *template files* where you need the info? I read the question so that you just wanted re-routing.
Boldewyn
It's the template where I need the info
+2  A: 

Can you use the Wordpress get_the_category function to grab the ID (from member variable cat_ID) once you're in the template?

See http://codex.wordpress.org/Function_Reference/get_the_category

e.g.

    foreach((get_the_category()) as $category) { 
        $id = $category->cat_ID;   
        // do something with $id
    }
Kris C
Looks like this would do it.
Boldewyn
+1  A: 

The thing is that the guy might not really want to associate categories with post ids. In this case, there's the global $wp->query_vars array that contains all the data coming from the GET request even when the routing has been "prettified"

OP does talk about wanting to get the post ID though: "?cat=4". If hypothetically there is other data in the query string that he's trying to use, then yes $wp->query_vars would make it available
Kris C