I want’s to add conditional body classes for index.php, page.php, category.php, tag.php
- In index.php body class only want’s <body class"home"> In
page.php body class only want’s <body class"page"> In category.php body class only want’s <body class"cat"> In tag.php body class only want’s <body class"tag"> In search.php body class only want’s <body class"search">
3
Answers
If you write the
body
tag in your template (usually in theheader.php
file) as follows , these classes will be added automatically:(note that the category class will be "category", not "cat")
You can use the
body_class
filter to accomplish this. Example below:WordPress docs: https://developer.wordpress.org/reference/hooks/body_class/
It would be best if you put "body_class" function in the body tag
then you can add classes to your pages using "body_class" filter
Your class will show up in this tag
For Reference: https://developer.wordpress.org/reference/hooks/body_class/