Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [body-class]

The `body_class` is a function that is meant to add classes to the body of a page, depending on the features of that page.

-1
votes
1answer
58 views

How to Add Body Class, and Remember/Retrieve it Through Local Storage

I want to create a system, where upon click of a button, a body class is added and the value is stored in local storage. The value is retrieved from the local storage, and is put again in the body ...
1
vote
1answer
30 views

Adding body class to login page?

I can't figure out for the life of me how to add a custom body class to the WordPress login page. I found this thread, which suggests using the admin_body_class along with this one to check if the ...
0
votes
2answers
141 views

Cannot set property 'className' of null at setThemeFromCookie

I am coding in WordPress to make a button to toggle body class from light-mode to dark-mode. i was experimenting with trying to add a cookie so that the preference is remembered for the browser. But i ...
0
votes
0answers
37 views

Adding body class in author page for custom role

I am trying to add a unique body class using body_class filter if the role is a custom one via my plugin. I have a custom role called trip_vendor and the url for its profile page is abc.com/operator/...
1
vote
1answer
31 views

Handling Body class based on Template

Through a PHP function, I think I can handle body class, but I think WordPress would have some specific way to handle it. If Home.php then class in the body should be wbody else it should be bgody. ...
0
votes
1answer
54 views

add class to all images inside the content

I have a function like this : function add_responsive_class($content) { $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); if (!empty($content)) { $document = new ...
0
votes
1answer
45 views

Body class to each level of a hierarchical custom taxonomy

How would this be adapted to apply to specific custom taxonomies? add_filter( 'body_class', 'custom_cat_archiev_class' ); function custom_cat_archiev_class( $classes ) { if ( is_category() ) { ...
0
votes
1answer
19 views

Adding body class to html tag that already has language attrubutes?

In my theme (Underscores based). I have <html <?php language_attributes(); ?>> Then I'm calling the body class function on the body tag: <body <?php body_class(); ?>> ...
0
votes
1answer
54 views

Update body class based on theme as well as a html attribute

I just started using wordpress. Now I'm trying to create a theme for a static web page I wrote. For that web page I'm using barbajs, which is why I have a html tag (in my case a section tag) with a ...
0
votes
1answer
28 views

How to use a custom body_class as a condition?

I am using a plugin that works great apart from the fact the breadcrumb is messed up. I would like to either try to ovveride the breadcrumb or remove it completely if I don't succeed. I am using ...
0
votes
1answer
21 views

How do i add a unique body class to the wordpress dashboard's home page?

I installed a plugin that let me add my own css and html to the home page of the dashboard. This is great! However some of my styles are messing up other pages. I could solve this if the home page of ...
1
vote
2answers
82 views

Add content as soon <body> starts

So, what I am trying to do is creating a slideshow plugin that won't require end user to write a single line of code. I can't use custom posts because I need to keep it simple for the end user. Is ...
1
vote
2answers
36 views

Append class to posts page

I'm trying to style something unique to the Blog/Posts page, which isn't on the front page. The body tag right now looks like this:<body <?php body_class() ?>> But doing this <body <...
0
votes
1answer
148 views

Add post id as body class in wordpress admin dashboard [duplicate]

i am looking to add custom class names to the body tag of all our posts (including pages, posts, Woocommerce products) all have post id's (seen in the URL when editing a post, category, etc.) and is ...
1
vote
1answer
28 views

De-bloating the page classes

The standard page class in Wordpress looks something like this; <body class="page-template page-template-page-templates page-template-page-standard page-template-page-templatespage-standard-php ...
0
votes
2answers
434 views

body_class(); not working with bootstrap navbar fixed top?

I am trying to use WordPress with a bootstrap template, class navbar-fixed-top is conflicting with body_class(); Is there a workaround to this problem? Edit 1: Additional details, I can't see the ...
0
votes
2answers
59 views

Add class when more page is visited

I would like to add a CSS class to the body if a visitor goes to the second or third etc. page of the website. Or vice-versa: add a class only on the first visited page. The goal is that I can ...
-1
votes
2answers
4k views

Add a unique class to HTML tag/element

I know there is a body_class function for WordPress. But is there one (or a way) to add a class to the HTML element? My goal is to be able to add a unique class (or ID) to a page's HTML element. ...
1
vote
1answer
547 views

I am adding a new class to my body tag if the logged in user is subscriber, need help

Here's what I am doing right now. In my header.php file: <?php if ( current_user_can( 'subscriber' ) ){ textdomain_body_classes(); } else { ...
1
vote
2answers
335 views

category hierarchy level as a body class - parent cat =1, child cat=2, grandchild=3

I'm finding this next to impossible to find any info on. I'm looking for a way to assign each category level a number and then add that number to the body class. e.g. the parent category archive would ...
2
votes
2answers
538 views

Why isn't is_page_template() adding a body class?

I want to conditionally add a body class depending on what template is being used. I can't figure out why the following code is not working... function damsonhomes_body_classes( $classes ) { if (...
0
votes
1answer
521 views

Creating custom post-listing templates in twentyseventeen child theme

I'm using a child theme based on 'Twenty Seventeen' and have created a custom post type ('review'). I want to make two Pages, one which lists 'reviews' and the other which lists a mixture of 'posts' ...
-2
votes
2answers
29 views

body class according to number of published custom posts

Is it possible to add a number to the body class according to how many custom posts a current user has published, e.g. CUSTOMPOST-4
0
votes
1answer
302 views

different body classes for each category

How can I add special classes ( like " japan japan_index" ) for body class in category-japan.php and for category.php something like this " archive archive_index " and also I'm using this code to put ...
0
votes
1answer
613 views

add css to only body text

In my single wordpress posts I want to apply a padding of 100px to the left and right. The problem is that when I apply it to .single .post-content the images also get a padding. However, I want all ...
0
votes
1answer
60 views

Adding theme option values as custom body class

I'm looking to add some custom classes to the body element based on theme options. I've setup two theme options ('rounded_corner_radio' & 'gradient_radio') that successfully output their values ...
3
votes
1answer
4k views

Remove and add class with body_class() function

I want to remove the default body class on the theme and add a new class. I use this code to do this: //REMOVE / ADD HOME CLASS FROM BODY add_filter('body_class', function (array $classes) { ...
0
votes
0answers
50 views

mu-plugins body_class filter not working

So this code was working fine in mu-plugins until an update back in september or so... function save_cookie_class( $classes ) { array_push( $classes, isset($_COOKIE['some_cookie']) ); ...
-1
votes
1answer
380 views

Change body_class() PAGE to HOME

I have a static Page Template with new updated post and i need change the body class page to home, the actualy class is: page page-id-2 page-template page-template-page-update page-template-page-...
0
votes
0answers
427 views

Remove body class per page template

I want to remove a body class based on a page template, but I want the control to be per template. My solution was create a function that removes the body class, then place that inside a function that ...
0
votes
1answer
97 views

Calling body_class and wp_nav_menu for a different post

I need to be able to display wp_nav_menu and body_class for a page/post that isn't the current post. For instance: // current page/post is ID:100 wp_query/query_posts{ // currently looping a ...
1
vote
0answers
2k views

body_class not working anymore?

I always used the following code to add a class to the body element of posts. //Page Slug Body Class function add_slug_body_class( $classes ) { global $post; if ( isset( $post ) ) { $classes[] = $...
1
vote
2answers
638 views

Add category to body class

I want to add the currenty category to the body class. How do I need to modify my current code, to add also the current category as a body class to the is_singular( 'knowledgebase' ) section? My ...
0
votes
1answer
61 views

adding a custom time class to <body> in theme twenty sixteen

I'm moving on from the child of a (no-longer serviced) 2009 theme to a child of twenty sixteen - I previously had a code in my functions that generated the week number as a body class - e.g w32 (for ...
5
votes
1answer
429 views

Add post class to the TinyMCE iframe?

I want to add styles to editor-style.css, based on the category the current post belongs to. Therefore I would like to add the post classes - using get_post_class() - to the body of the TinyMCE ...
-1
votes
1answer
544 views

Non Object Notice Error - How to fix please

No doubt I'm missing something obvious here... The following code to add a body class to an admin page is producing this error notice: Notice: Trying to get property of non-object in /.../box.php on ...
0
votes
1answer
60 views

How do I add Category Body Classes to Admin?

So why are category classes not added to the admin body class with this function? I see the current post ID in page source as part of the complete body class, so the basic function of adding body ...
0
votes
1answer
82 views

Wordpress blog assign unqiue body class

I am using a function to change the body class depending on whether the user is on the home or sub page. Currently everything is working as it should, the homepage is showing the home background and ...
1
vote
1answer
322 views

Wordpress php filter admin_body_class not working

I have the following function in a plugin I have built: static function filter_add_body_classes( $body_classes ) { $new_classes = array(); // Only when logged in if ( ! ...
1
vote
1answer
332 views

body_class REST field in WP-API

I'm building a site with WP-API (v2). I'm attempting to add a REST field according to documentation that includes anything from body_class. However, body_class is returning either one class (one ...
0
votes
1answer
435 views

How to list out all values from the get_body_class array

I am trying to list out all of the current page body classes. using the body_class function it gave me class="[body classes were here]" however I need to show these as plain text. I have tried the ...
2
votes
1answer
3k views

Adding a body class with ACF

I have created a radio button field with ACF so that admin can specify a colour. E.g: blue : Blue pink : Pink I'd like this to be output as a class via <body <?php body_class(); ?> in ...
0
votes
1answer
822 views

Add parent ID to body_class

What's the best way to add the Parent ID of a page to the <body <?php body_class(); ?>>function? I am currently using the below function to get the Parent ID of a menu item for a ...
0
votes
2answers
555 views

Custom Taxonomies in body class with parent slug

I have tried many answers here on stackoverflow but couldnt find a solution to my problem. I have created a custom post type called ph_products and custom taxonomies with the name ph_category. Then I ...
0
votes
1answer
360 views

How to add same body class in multiple pages using their page id?

add_filter( 'body_class', 'custom_body_class' ); function custom_body_class( $classes ) { if ( is_page( '38034' ) ) { $classes[] = 'new-class'; return $classes; } } Above ...
1
vote
1answer
795 views

create a page which displays a list of categories title+ short description?

I have a code that loops through all categories and: 1.organize them on a Hierarchy of (parent) category and its child categories 2.echo the titles +short description of each category +add "read ...
0
votes
3answers
2k views

two body tags on all WP pages regardless of theme

I have a wierd problem. If I inspect my WP pages using chrome element inspector I see two body tags thus: <body cz-shortcut-listen="true"> <body> This bit is to do with colorzilla (...
0
votes
1answer
6k views

How to assign a class to a page with a custom template?

I tried with the bellow code to assign a body class to a page with a custom template, but this didn't work. What is wrong here? function prefix_conditional_body_class($classes) { if ( ...
1
vote
1answer
586 views

body_class filter - Is there a better way to write this code?

I would like to add a body class depending on what sidebars are active, and a body class if my before-header widget area is active. I have got it to work, but I would just like to know if there is a ...