Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different CSS for homepage

Tags:

css

php

wordpress

Ok, based on the answer here by traitadmin (scroll down) I am trying to display different CSS for my front page and one for all other pages.

Previously, my body tag looked like this:

<body <?php  body_class() ?>> 

Now, I have edited it to look like this:

<body <?php if (is_home()) { ?> id="home"<?php } else { ?> body_class()<?php } ?> >

Also I have put new stylesheets in my enqueue, and all styles for the homepage now have #home in front of them.

However, there is no style at all now and my site broke down entirely, for all pages.

I think I may need body_class() also for the homepage. But how I can edit this code so that it works?

like image 308
Forza Avatar asked Feb 28 '26 03:02

Forza


1 Answers

Easiest would actually be this:

<link rel="stylesheet" href="main.css">
<?php if (is_home()) {
    echo '<link rel="stylesheet" href="home.css">'
} ?>

Rules in home.css should have a high priority than main.css. So with this if main.css has this:

body { background-color: white; }

and home.css has this:

body { background-color: blue; }

Since home.css is linked after main.css the rule in home.css will take priority and the background will be blue. This way you can only overwrite the styles you need to and all the other styles will still apply.

In your case after your other wp_register_style() just add an if test and then register another style.

if (is_home()) {
    wp_register_style('home-style',get_stylesheet_directory_uri().'/stylesheets/home.css');
}
like image 166
Pitchinnate Avatar answered Mar 01 '26 15:03

Pitchinnate



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!