Tom Geoco
Tom Geoco

Reputation: 815

How do I create per page and per category stylesheets in Wordpress?

I'm trying to create many different skins that apply depending on the page and the category of my Wordpress site. I am not sure of two things:

  1. How to use an or statement in PHP to call on the skin1.css stylesheet if is_page('x') OR if is_category('1'). (On that note, how do you specify multiple pages and/or categories in the same line?
  2. Why is the following code displaying as text in the header of my web site?
  3. There must be a better way to write these if else statements. Any tips?

I know this is a mess.

    < ?php if (is_page( 'health' ) ) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/skins/health-skin.css" />
    < ?php } elseif (is_page( 'beauty' ) ) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/skins/beauty-skin.css" />
    < ?php } elseif (is_page( 'home' ) ) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/skins/home-skin.css" />
    < ?php } elseif (is_page( 'food' ) ) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/skins/food-skin.css" />
    < ?php } elseif (is_page( 'travel' ) ) { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/skins/travel-skin.css" />
    < ?php } else { ?>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/css/skins/default-skin.css" />
    < ?php } ?>

Upvotes: 0

Views: 482

Answers (2)

Tom Geoco
Tom Geoco

Reputation: 815

So, I got this to work.

Originally I had used is_page('x') and then switched to is_category('x').

That worked for the categories, explicitly. When a post belonging to one of those categories was visited, the stylesheet defaulted back to the original setting.

It took some time and reading, but I found something that worked quite well (and looks prettier with the use of arrays).

 <?php if ( !is_home() && in_category( array( 'health-fitness', 'fitness', 'health-fitness-blogs', 'menopause', 'nutrition', 'vitaminminerals-chart', 'weight-management', 'health' ) )) { ?>
 <link rel="stylesheet" href="<?php bloginfo('stylesheet_directory'); ?>/css/skins/health-skin.css" type="text/css" media="screen" />
 <?php } ?>

I added the !is_home() because something was throwing off my home page from the default setting and pulling in a specific stylesheet. I'm wondering if because the home page was the "front" page for the content that the in_category('x') was registering some of the excerpts as part of those equations.

It's just a guess. I don't actually know, but that seemed to resolve the issue so far.

Thanks again for all the help.

Upvotes: 0

Ol Sen
Ol Sen

Reputation: 3348

&&  //=and
||  //=or
!== //=not same as
!   //=not

http://www.w3schools.com/php/php_operators.asp

Upvotes: 1

Related Questions