|

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

  • |

    WordPress Cyber Weekend Deals 2018

    Well it’s that time of year, where there are lots of deals that I’d rather buy for myself than for others. I think nothing better encapsulates that than the digital deals going on in the WordPress /online worker space. I mean don’t get me wrong, I’d love to get a great deal on hosting. But…

  • | |

    Random Code Generator 1.0

    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!A couple of weeks ago, when I officially launched the redesign, I mentioned the forthcoming release of a plugin I wrote…

  • |

    Using Back Tap on iPhone

    I had the pleasure of participating in the GoWP Niche Agency Owners Happiness Hour recently and we talked all about tools (I didn’t know that going in, but as you can imagine I was pleased). Something I got to demo was how to turn on iPhone Back Tap (a feature in iOS 14 and up)….

  • |

    Upcoming Speaking Events

    As it turns out, I’m speaking a couple of times next week on WordPress Custom Post Types- check out the information below and register if you’d like! Connecting Custom Post Types WP Summit, Online, June 11th, 2013 at 12pm CST Use the code 20JOE for 20% off. DIY Custom Post Types NEPA WP Meetup, Library…

  • I’m Speaking at WordCamp Baltimore!

    The last WordCamp I went to was WordCamp Chicago in June, with it rounding out a big tour that started in January to promote my book, Responsive Design with WordPress. While I took a much needed break (and started a new job thanks in part to that trip), I’m really excited to announce that I’ll…

  • |

    Quick Tip: How to Un-shrink a Sweater

    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!So this isn’t really tech related, but I’m a guy and not a very bright one at that. Last week a…

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.