|

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

  • | |

    Standard WordPress Plugins

    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! Over the last few weeks I’ve been working a lot with WordPress. Between a new job I started, Freelancing the…

  • My Slides from WordCamp Philly 2012

    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!Here you go! The slides from my talk, “Building a Simple WordPress Framework” at @WordCampPhilly are available for download, as well…

  • Tools I’m Using to Avoid Spoilers

    Between a certain Game of Thrones and Avengers: Endgame, I’ve been seeing many complaints about things being spoiler on social media. Even with the lengths I went through when Infinity War came out, it was still spoiled because a careless YouTuber (me) let autoplay happen unregulated, and another YouTuber (the guy in the video) opened…

  • |

    What is a Hackathon?

    According to Wikipedia, a hackathon is defined as: …an event in which computer programmers and others involved in software development, including graphic designers, interface designers and project managers, collaborate intensively on software projects. Basically, it’s a place were people who work with computers can come together to hopefully make something awesome. There are all types…

  • 100 Words 040

    For today’s 100 Words, I’d like to deflect to the Organizers of WordCamp US. It’s sure to be a great event with lots of amazing people, and the first one outside of San Fransisco. If you can come you definitely should. I know I’ll be there! WordCamp US 2015, held December 4–6 in Philadelphia, Pennsylvania,…

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.