How to create specific CSS selector base on WP Post Type and Post IDWordpress Theme tips

  • While spending my time stalking wordpress support forum, I stumbled on this 6 days old unresolved topics ” using page/post id to specify per-page css in 2.5″.

    chibi hate you mod by chaoskaizerI’m about to upgrade a site from Wordpress 2.3 to the latest version. I’ve previously been employing the page id in order to have page specific CSS, e.g., pages with either 0, 1 or 2 sidebars.

    For example, if the url is http://site.com/page1, the page is styled with a combination of a template and CSS to specify the width of primary:

    #page1 #primary {margin-left: 0;width: 890px; }

    I’ve read in some other posts here that Wordpress 2.5 no longer uses this, and now the post ID is used instead [...] ~ hauntedtapedeck

    Workaround

    Basically, what we need is a unique CSS Selector for specific post & custom page inside the template.

    firebug html console

    1. First we create a function for our CSS selector and save it inside WP theme functions.php

      functions.php: get_post_selector_classname()

      function get_post_selector_classname()
      { global $wp_query;
      
       if (!is_object($wp_query) ) return;
      
       if ($wp_query->is_single || $wp_query->is_page) {	 
      
      	 $pid = $wp_query->post->ID;
      	 $post_type = $wp_query->post->post_type; 
      
      	 return 'wp-'.$post_type.' '.$post_type.'-'.$pid;
       }
      }
      
    2. Next we call the get_post_selector_classname function inside the template. You can call this function anywhere inside your template but the best placement for the class selector is inside body tag (more weight for inheritance).

      header.php

      <body class="<?php echo get_post_selector_classname();?>">
      

    Available CSS selector

    The following CSS selector is available inside WP post single and page only.

    • body.wp-page
    • body.page-ID
    • body.wp-post
    • body.post-ID
    CSS Example

    styling “single page” post.

    body.wp-single{background-color:#f6f6f6;}

    styling a single page with post ID 69.

    body.single-69{background-color:#f6f6f6;}

    styling custom page with post ID 42.

    body.page-42{background-color:#f6f6f6;}

    styling all custom page.

    body.wp-page{background-color:#f6f6f6;}

    Wordpress 2.6.2

    I’m not sure why its not working thought. For WP 2.6.2, try the below code

    // add inside functions.php
    function post_selector_classname()
    { global $wp_query;
    
    	$output = 'wp-';
    
     if ($wp_query->is_single || $wp_query->is_page) {	 
    
    	 $pid = $wp_query->post->ID;
    	 $post_type = $wp_query->post->post_type; 
    
    	 $output .= $post_type.' '.$post_type.'-'.$pid;
     } elseif($wp_query->is_home) {
     		$output .= 'home';
     }
    
     echo $output;
    }
    

    call inside template (header.php)

    <body class="<?php post_selector_classname();?>">
    

    Refer query.php inside wp-includes folder for more WP hierarchical sections.

    About the Author
     

6 Responsesto “How to create specific CSS selector base on WP Post Type and Post ID”

    • Jonathan's photo
    • RE:How to create specific CSS selector base on WP Post Type and Post ID
      5 months, 4 weeks ago on at 3:39 pm3url · microId

      Hi!

      First off, thanks so much for taking the time to address this. It was me who posted the original query.

      I’m working my way through your instructions, and have come up against a problem with the second part here. I’m using a modified version of K2, and in header.php, it already has this:

      <body class=”">

      How should i go about applying your modification?

      Thanks!

      [Reply]
    • stalker's photo Kaizeku Ban
    • How to create specific CSS selector base on WP Post Type and Post ID - 'Comment Guidlines' ↓
      5 months, 3 weeks ago on Wednesday, July 16th, 2008 at 6:55 am 5 url

      If you want to comment, please read the following guidelines. These are designed to protect you and other users of the site.

      1. Be relevant: Your comment should be a thoughtful contribution to the subject of the entry. Keep your comments constructive and polite.
      2. No advertising or spamming: Do not use the comment feature to promote commercial entities/products, affiliates services or websites. You are allowed to post a link as long as it's relevant to the entry.
      3. Keep within the law: Do not link to offensive or illegal content websites. Do not make any defamatory or disparaging comments which might damage the reputation of a person or organisation.
      4. Privacy: Do not post any personal information relating to yourself or anyone else (i.e., address, place of employment, telephone or mobile number or email address).

      In order to keep these experiences enjoyable and interesting for all of our users, we ask that you follow the above guidlines. Feel free to engage, ask questions, and tell us what you are thinking! insightful comments are most welcomed.

Subscribe to this discussion via RSS

1 2

"write as if you were talking to a good friend (in front of your mother)."

.haveyoursay

Disclaimer: For any content that you post, you hereby grant to Kaizeku Ban the royalty-free, irrevocable, perpetual, exclusive and fully sublicensable license to use, reproduce, modify, adapt, publish, translate, create derivative works from, distribute, perform and display such content in whole or in part, world-wide and to incorporate it in other works, in any form, media or technology now known or later developed. Some rights reserved.