|

Quick Tip: Use Post or Page Slug as CSS Class in WordPress

I don’t know why, but for some reason, I thought WordPress already included the slug of a post or page in either the function body_class() or post_class(). Doing some work over this weekend I realized that’s not the case, but luckily, it’s very easy to do.

The functions body_class() and post_class() serve as a way for you to easily customize a page’s CSS based on attributes of the page being viewed. They will output things like the post ID, the custom post type, if the page is the blog, home, or front page, and more. One thing it does not include, however, is the post slug. This could be useful if you want to style each of your posts a certain way, or in a more likely scenario, style each page a certain way. If you want to do this, the code is fairly straight forward; both functions accept an option argument: a string of your own classes. We use that combine with the $post array, like so:

<?php body_class($post->post_name); ?>

or, if you want to do it on a post-by-post basis:

<?php post_class($post->post_name); ?>

Note the latter must be used inside the Loop. Using this, if you have a page named, “Services” with the slug services, the output would look something like this:

<body class="page page-id-108 page-parent page-template-default services">

Now you can use the .services (or whatever) class name to do some custom stuff instead of needing to know the page or post ID.

 

Similar Posts

  • |

    I’m Working at Crowd Favorite

    …or, “I really can’t wait to work for Chris Lema.”* Today, I’m very excited to announce that starting in September, I will be a Front End Developer at Crowd Favorite.  I have been following the company since Alex King started it, love the work they do, and could not be happier to join a team…

  • |

    WordPress Plugins I Can’t Live Without

    I work with WordPress on a daily basis. By working within my own sites, setting up new sites, and testing new things, I come across a lot of WordPress tools. As a result, I have a group of WordPress plugins that I’ve come to rely on. There are some that are site specific (like WooCommerce…

  • How Do I Learn Web Development?

    A couple of years ago, I was on the How to Hold a Pencil podcast (episode here) and we discussed a number of things, including the steps I took to learn web development (over 14 years ago…whoa). I’m often asked, as I was on the show, what I recommend for people starting today. Here’s my…

  • |

    Simplecast Episodes Version 0.6

    Over the weekend I released Version 0.6 of my @simplecast plugin, Simplecast Episodes. Aside from some smaller internal changes, I added what I think is a pretty nice feature to coincide with WordPress 3.9’s new feature to create media playlists from audio and video: the ability to download episodes directly into WordPress, adding them to the…

  • |

    Win a Free Ticket to WP Summit!

    Last week I talked about the tail end of my speaking tour (thanks to everyone who came out to @WordCampPhilly). Up next is @WordCampChicago, but I’ll also be speaking at the WP Summit 2014, an online conference, on Wednesday, June 18th and I want to give away a free ticket!

6 Comments

  1. You can add this into the functions.php file.

    //Page Slug Body Class
    function add_slug_body_class( $classes ) {
    global $post;
    if ( isset( $post ) ) {
    $classes[] = $post->post_type . ‘-‘ . $post->post_name;
    }
    return $classes;
    }
    add_filter( ‘body_class’, ‘add_slug_body_class’ );

  2. Hi Joe,
    I was looking for something like this! Well, at least I hope this would take care of what I need.
    Can this aggregate posts in a certain category (“studies”) into a grid format? My current theme stacks the blog posts one on top of the other, and I need them to be on a grid of 4-5 feature images with a 2 line description below the feature image, per row, with rows going down. Can this be done with the code above?

    I got to you because I am looking at the Rockable’s Build WP themes from scratch book you did, and I thought it perfect you writing this code above at just the right time for me to find it 🙂
    Thanks!

  3. Monica, thanks for reading! If I am not mistaken, the categories are already added in post_class() so if you use that call, and each blog post is in an article element, you should be able to do something like .studies article{ //css here }.

  4. Hi there, You’ve done a great job. I will definitely digg it and personally recommend to my
    friends. I’m confident they will be benefited from this website.

  5. Hi, i believe that i noticed you visited my weblog so i got here to
    return the prefer?.I’m attempting to to find things to improve my site!I
    suppose its ok to use a few of your ideas!!

    my page; sun dolphin kayak Review (youtube.com)

Comments are closed.