Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a permalink structure that looks like this,

%category%/%postname%

I have a category.php template coded up and and trying to pull of the post of a certain category, so for example my URL may look like this,

/category/category1

I want all posts that are in category1 to be return however, when using the following code I get null returned,

get_query_var('cat')

I assume that this code is looking for URL that looks like this,

?cat=category1

So how do I make it work with permalinks?

share|improve this question

1 Answer

up vote 1 down vote accepted

WordPress rewrites rules invisibly translate pretty permalinks to the non-pretty format internally, and set the appropriate variables and load the requested page, you don't need to do anything in your template to load posts from a category on a category page.

The cat query var specifically will be set to the ID of the requested category, not the name.

That said, this:

$my_category = get_query_var('cat');
echo $my_category;

should print the selected category ID on a category page. If it's not, we'd have to see your code to help you further.

Also, have a look at the $wp_query global to see all query vars set on a particular page:

global $wp_query
print_r($wp_query);
share|improve this answer
Where exactly does the query_var show up in the print_r($wp_query)? – AlxVallejo Sep 27 '12 at 14:38

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.