开发者

Wordpress URL routing problem

开发者 https://www.devze.com 2022-12-19 01:06 出处:网络
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 pr

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 ?


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
    }


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"


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号