|

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 Speaking about WordPress on Saturday in Scranton!

    Come on down to the Vintage Theater at 9am Saturday to see me speak about the basics of WordPress! My talk is part of a 24 Hour bonanza that’s going on from Friday at 9pm to Saturday at 9pm. I will include art, music, movies, tech, and more! The Vintage Theater is located at 326 Spruce…

  • 3 Ways to Make the Most of Your WordCamp US Experience

    WordCamp US 2019 is next week and I’m getting excited. It’s the biggest US-based conference for people in the WordPress community and at this point, it feels a lot like a college reunion to me. But there are also goals I’d like to accomplish while I’m there. If you’re spending your own money to get…

  • |

    Quick Tip: Get a Family Cell Phone Plan to Save Money

    Note: This article was published while I was in my early 20s. I was much younger and dumber. Please don’t hold it against me. One of the perils of having a 20+ year old website!I was talking to a friend yesterday who told me his parents booted him and his brother off their family plan…

  • Offering WordPress Workshops

    An idea I had a while ago, around the time I moved back to Scranton, was to offer a WordPress workshop that would show people how to use the platform and possibly how to develop on it. Recently I was approached by some people who were interested in something similar. Now, how to use and…

  • 100 Words 014

    There’s drama in the WordPress community today, which happens about monthly seeming on the last day of the work week. Today’s is about being polite when shooting people down. This tweet by friend & instigator Ryan Duff is a good starting point. https://twitter.com/ryancduff/status/616583733861351424 The problem is many coders think: They are smarter than everyone. Everyone should…

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.