2 replies
Hello php/wordpress folks!

OK, I have a question that I think/hope is relatively easy to solve. To begin with, I'm not new to WP and have been using it for 3-4 years. My problem is I'm not great with PHP...just know a little.

I've got a Studiopress theme (not using the Genesis framework). The theme I'm using comes with 2 colour-styles, or layouts. I'm adding 10-14 additional colour schemes. So far so good. I know html/css very well so styling is easy for me.

However, since I don't know a lot about PHP, I'm trying to find the best way to include all the different stylesheets in the header.

Currently, since this theme only has 2 styles, it's using a simple if/elseif statement. No problem. However, since I'm adding so many styles, all I can think to do is have many elseif statements which I know isn't efficient coding.

Example:

Out of the box, the theme comes with this in the header....


<?php if(get_theme_mod('color_style') == 'Gray') { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } elseif(get_theme_mod('color_style') == 'Blue') { ?>

With all my new styles, I've got something like:

<?php if(get_theme_mod('color_style') == 'Gray') { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } elseif(get_theme_mod('color_style') == 'Blue') { ?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/blue.css" type="text/css" media="screen" />
<?php } elseif(get_theme_mod('color_style') == 'Red') { ?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/red.css" type="text/css" media="screen" />

etc.....

That's a lot of elseif statements if I have 10+ colour schemes. My initial (php newbie) thought is to create an include, ie <?php include('styles.php'); ?> and in styles.php have all my stylesheets. When someone selects a colour scheme in the theme options, it'll only pull the stylesheet of that particular colour scheme anyway (in the source code) but if I have up to 14 different styles, then in header.php it looks well...inefficient but, it's probably not a huge deal.

So, can anyone recommend how to more efficiently code the header? Is an include a good idea?

Ideally I'd just have something like:

<?php } elseif(get_theme_mod('color_style') == '<?php (get_option('color'))') { ?>
type="text/css" media="screen" />

or something like that where the selection would simply change within the one line of esleif but I don't know enough php to set this up in the functions.php (actually in the theme-options.php) page.

Does this make sense?

Any advice on how to SIMPLY and QUICKLY make this more efficient?

Thanks!!

Scott
#php #wordpress
  • Profile picture of the author SteveJohnson
    You'll have to make sure that you're consistent with your naming conventions, but the easiest way to do this is to use the option value as the stylesheet name (on two lines for clarity):

    PHP Code:
    <link rel="stylesheet" type="text/css" media="screen"
    href="<?php bloginfo('template_directory'); ?>/styles/<?php echo strtolower(get_theme_mod('color_style')); ?>.css" />
    use lowercase filenames for your css files to reduce confusion; transform the color style choice to lowercase in the address.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[2848649].message }}
  • Profile picture of the author promo_guy
    Steve, you're AWESOME!!

    Thank you very much. I knew it was going to be simple. At first I couldn't get it to work but, this is what I did (in addition to your code)...in case it helps someone else.

    I had (e.g.):

    <?php if(get_theme_mod('color_style') == 'Gray') { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <?php } elseif(get_theme_mod('color_style') == 'Blue') { ?>
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/blue.css" type="text/css" media="screen" />

    And I simply changed it to:

    <?php if(get_theme_mod('color_style') == 'Gray') { ?>
    <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
    <?php } elseif(get_theme_mod('color_style')) { ?> <--- Got rid of =='Blue'
    <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/styles/<?php echo strtolower(get_theme_mod('color_style')); ?>.css" type="text/css" media="screen" />

    So, I deleted == 'Blue' and added your code and it worked perfectly!

    Cheers buddy, much much appreciated

    Scott
    {{ DiscussionBoard.errors[2848781].message }}

Trending Topics