I believe the hook you’re looking for is body_class
or post_class
filter. These filter will allow you to remove or push classes into an array which will in turn display them on the body
tag or post tag respectively. To add the post slug we could do something like this:
/**
* Add, Remove, and Modify body classes
* @note http://wordpress.stackexchange.com/a/236106/7355
*
* @param Array $classes
*
* @return Array $classes
*/
function theme_body_classes( $classes ) {
global $post;
if( is_object( $post ) ) {
$classes[] = $post->post_name;
$classes[] = "{$post->post_type}-{$post->post_name}";
}
return $classes;
}
add_filter( 'body_class', 'theme_body_classes' );
Related Posts:
- Editing the Number at the end of Page URLs / Editing Page Slugs
- How can I get the page url slug when ‘post_name’ returns an id?
- get post author id outside loop
- Custom Walker: how to get ID in function start_lvl
- How to disable page delete
- get_pages() not ordering as it should
- Pagination Help on Crazy Custom Authors Page
- Create subpage /user/ or /my-profile/ like /author/ with additional query like /user/user123
- Hide page title in WordPress 3.0
- Selecting An image from the Media LIbrary on the frontend
- Custom protected password page with ACF
- Page load requires a trailing “/” [closed]
- WordPress 3.1 removing ‘category’ from the slug
- Virtual Pages plugins
- Programmatically add a custom page/path/url/route to WordPress
- What is the most idiomatic way to let clients edit phrases in HTML?
- How to merge all my pages into one, while keeping a menu bar?
- how to flush custom author rewrite rule
- How to change URL Custom Page?
- redirect product-category to a custom page
- Remove Author Slug & Replace With Username
- custom headers for static home page and posts page
- Show posts on a page based on a category and tag(s)
- Split Conference WordPress Page in different years
- WordPress slugs and 404 handling takes preference over folders in site root?
- Add google org chart to wordpress page
- How to get custom page template being edited in gutenberg editor
- Control Content On Non-Blog Pages
- custom meta box for page default template
- Best practice – pages – ACF or inline coding
- How to hook up search results page to a back-end page?
- How to structure model showcase
- How to customize
- Add a searchbox to the body of a page
- Create page as a file in my theme?
- Featured Image If Else Condition
- Don’t change Custom Post Type slug to unique value
- Get wordpress taxonomy slug name(s) to use as div class
- Insert menu with a custom walker into page / post body using shortcode?
- How to create “custom page types” with different custom fields in them?
- How to create custom search page
- Is it possible to restrict access to specific pages in the admin area based on the page slug?
- Creating php pages in wordpress and assigning them to wordpress pages
- How to create custom layouts and static pages in themes for clients
- How to make the Pages in WordPress Customized?
- Creating custom page template
- Change Site Logo or Menu Logo Per Page
- change top banner content on specific page
- How to add page content into another page
- Display a specified content on a Page and all its child pages
- How to get Previous Visited Page Information
- Give custom page template a custom post ID
- If Term Name Exists, Append a Unique ID to Slug
- Use a custom page with dynamic content as a user profile page
- I want to embed mystream video in my wordpress site
- Adding a custom field to a slug
- Swap home page based on user role, Elementor layout breaking
- Taxonomy template is not reading
- New Page/Post Screen Opens an Existing Post
- Static Page, need Last and Next links at the bottom
- How can I create a Custom Page? (not a page template!!)
- How to assign a default/priority taxonomy to be shown in url in case two taxonomy items are selected
- wordpress custom posts page not showing
- How to serve an already coded html page in a WP page, to assign a simpler url
- Admin menu disable new/delete page
- Show different pages on site depending on user choice
- WordPress post submit page
- Redirect Every Instance of The Author Template Pages to Custom Author Pages
- Allow arbitrary text after page URL
- Best way to create a page outside of the pages list
- Changing starting number of User ID
- How to keep the /blog slug even in single post slug
- Want home page to click through to two different page types
- Custom single page based on which custom page template is selected
- How to duplicate “Pages” section in the control panel?
- Page template with custom html fields inside a content
- User registration add user ID?
- Pass query param to custom page
- Is there a way to have information on one page automatically feed into another? [closed]
- how to add a page
- How to apply styles to the custom page template
- How can I make a search term redirect to a page
- Custom CSS working on home page but not on NEW created PAGE
- Create a page for each database table entries
- How to set post slug when using wp_insert_post();?
- Remove WordPress theme from a specific page
- Why would switch_to_blog stop working?
- $wp_customize->remove_section for customizer setting?
- Fatal error: Call to a member function add_rewrite_tag() on a non-object in /wp-includes/taxonomy.php on line 333
- Change login error messages? Cookie error message
- Multiple Images/ Slider with custom post [single.php] [closed]
- website with pretty permalinks except pagination
- Custom login and registration forms
- WordPress in a Subdirectory
- What is the best way to make a custom page from a plugin?
- javascript file not working in a plugin template file
- How can I display both LTR and RTL language texts on the same page?
- A specific menu inside a page, load different contents without reloading it, under the same link
- How to define a static Home page
- Custom Search on media files PDF images pages posts