I’m having trouble with conditional CSS loading in WordPress. I want to load different stylesheets for different pages, but my condition always goes to the else statement no matter what page I’m on.
When I visit my homepage, I expect it to load homepage.css but it keeps loading default.css instead. I can tell because the colors are different between the two files. Even when I switch the CSS files around in the conditions, it still always hits the else part.
What am I doing wrong with this setup? Is there a better way to load specific CSS files for specific WordPress pages?
yeah, classic wordpress gotcha. use is_page() instead - is_page_template() doesn’t always work like you’d expect. also double-check you’re actually using a page template, not just the default homepage setup. quick debug: throw an echo 'homepage loaded'; in your condition to see if it’s even triggering.
The problem is home.php handles your blog posts page, not the front page. WordPress uses front-page.php for the actual homepage. If you want to target the front page, use is_front_page() instead of is_page_template(). Also check Settings > Reading - if your homepage shows posts instead of a static page, WordPress uses home.php for the blog listing and you’d need is_home() as your conditional. I hit this same issue when I started with WordPress - the naming’s confusing. Check your reading settings first, then fix your conditional function.
Had this exact issue and it drove me nuts for hours. Your template file probably isn’t named what you think it is, or you’re using the wrong conditional function. Try is_front_page() if you’re targeting the homepage, or is_page('page-slug') for a specific page. Double-check your template is actually called home.php and not front-page.php or index.php. Debug this by adding var_dump(get_page_template_slug()) to see what template’s actually being used. Also verify you’re on the page you think you are - WordPress sometimes uses different templates than expected depending on your settings.