WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to insert the category or page ID of the current page into my HTML so that I can use CSS to style each page based on what it is.

For example, if had a category called "News" and it's ID was 5, my source code would end up having in it. Then I could use CSS to style #category-id-5 to suit my needs.

How would I accomplish this?

share|improve this question
up vote 2 down vote accepted

get_queried_object() has a property with that ID.

But you don't really need it, because the <body> tag should have a class with the current category / page ID...

share|improve this answer
For whatever reason the theme I'm using didn't have this functionality already added to the body tag. And unfortunately get_queried_object() didn't work for me. <?php $my-page = $wp_query -> get_queried_object() ?> <body class="my-page-<?php echo $my-page ?>"> – jimmykup Oct 1 '11 at 5:40
it should be my_page->ID, and my_page->term_id for categories – onetrickpony Oct 1 '11 at 7:09

Your Answer


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.