Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to do the following

I need to get the category of the current page at the plugin level so before it goes to archive.php orsingle.php

so i have a plugin to do this and based upon category it switches themes .. however i am stuggling to get the category at the plugin level when i am using custom permalinks which do not have a query string (ie seo friendly urls)

So if i go to the default permalinks , I can get the category within the plugin using

$wp_query->query_vars['cat'];

However with the custom permalinks that returns null and I have not been able to think of another way of doing this ,

Please can anyone help ?

Thanks a lot

Waseem

share|improve this question
    
There is not enough detail here to answer the question. "What plugin are you using?", for example. Post your relevant code alsow. –  s_ha_dum Jan 6 '13 at 16:19

1 Answer 1

When pretty permalinks are enabled, the query var is category_name:

$wp_query->query_vars['category_name'];

or use get_queried_object:

$this_category = get_queried_object();
echo $this_category->term_id;
echo $this_category->name;
echo $this_category->slug;
// etc..
share|improve this answer
    
that solution doesnt work .. $wp_query->query_vars['category_name']; gives null, the other option gives Fatal error: Call to a member function get_queried_object() –  Waseem Aslam Jan 6 '13 at 18:16
    
then you'll have to show the context in which you're attempting to use this code –  Milo Jan 6 '13 at 18:19
    
also, be more specific about what type of query you're trying this on- a category page? if it's a single post, there will be no category query var, as you're not querying a category. –  Milo Jan 6 '13 at 18:23
    
ok context of the plugin is .. based on category of either archive page or single post page.. i want to show different template , i have all the other stuff working fine .. all i need now is an elegant way to detect category on both these pages starting with the archive page. Its fine using standard permalinks , with non standard ie custom permalinks i am getting null for everything. So ignoring what i do with the category id later on my code is currently var_dump($wp_query->query_vars['category_name']); which gives null. i need the cat id...plugin is activated... etc –  Waseem Aslam Jan 6 '13 at 22:23
1  
the context meaning where are you attempting to put this code, what action, or is this in a template? have you globalized $wp_query? –  Milo Jan 7 '13 at 1:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.