ThemeShaper Forums » Thematic

using multiple stylesheets in a child theme

(8 posts)
  • Started 11 years ago by anna
  • Latest reply from Anthrax
  • This topic is resolved

Tags:

  1. anna
    Member

    Hi Ian,

    In your helpful and interesting article http://themeshaper.com/functions-php-wordpress-child-themes/ on child themes you mentioned "How To Add a Custom Stylesheet To Your WordPress Child Theme". In your example you used this code for an IEfix

    function childtheme_iefix() { ?>
    <!--[if lt IE 8]>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/ie.css" />
    <![endif]-->
    <?php }

    add_action('wp_head', 'childtheme_iefix');

    but you mentioned that it could be adapted to have different stylesheets for various parts of a site by using the conditional tags

    my question and I'm sorry I don't have the knowledge to work this out for myself, is if I wanted to modify functions.php in my child theme so as to load a different stylesheet for a specific page or page template, what would the code need to be?

    something like this?

    <?php
    function childtheme_css() { ?>
    <?php if(is_page('1')) <link rel="stylesheet" type="text/css" href="<?php bloginfo('wpurl'); ?>/wp-content/themes/candy/style3.css" /> ?>
    <?php }
    add_action('wp_head', 'childtheme_css');
    ?>

    (I know the above is wrong - as I don't know how to actually write this correctly)

    if you can provide any help with this, I would really appreciate it.

    Posted 11 years ago #
  2. function childtheme_css() {
      <link rel="stylesheet" type="text/css" href="<?php bloginfo('wpurl'); ?>/wp-content/themes/candy/style3.css" /> ?>
    }
    if (is_page('1')) {
      add_action('wp_head', 'childtheme_css');
    }

    That should work. Or you could just target the pageid-1 body class in your stylesheet.

    Posted 11 years ago #
  3. Hi Ian,
    I'm trying to do exactly this, but I can't get it to work. For some reason, the conditional statement won't work. If I comment it out my custom stylesheet applies to all pages, so that works, but with the conditonal statement in I can't get it to apply selectively to anything. Here's the code:

    <?php
    function childtheme_css() { ?>;
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/homepage.css" /> <?php };
    if (is_page('2')) {
    add_action('wp_head', 'childtheme_css');
    }
    ?>

    I don't really have much of a clue about php, so it probably contains some elementary error. If you, or anyone else can spot it, I'd really appreciate some help.

    Cheers

    Ken

    Posted 10 years ago #
  4. Hey Ken,

    try this one:


    function childtheme_css() {
    if (is_page('82')) {?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/homepage.css" /> <?php }
    }
    add_action('wp_head', 'childtheme_css');

    The if (whatever-condition-is-true) {do_action .. won't work. The action would be added as soon as the condition is true .. and it stays added forever.

    So if you need something conditional, you have to include it inside of your action. The action will be added and each time a new page is opened it'll check for the condition and the CSS will be loaded or not.

    Cheers,

    Chris

    Posted 10 years ago #
  5. Chris,
    If I was planning on having any more babies, I would name them after you.
    Thanks for your help.
    Ken

    Posted 10 years ago #
  6. I try the code from chris but i have a problem to get this work. I change is_page('83') to is_single() || is_page()

    I have changed the postheader with a filter. On the start Page it looks OK but when you choose a single Post (that ist displayed without the date an comments-link on the left side) the display is not correct.

    Need a single.css and a page.css but i don't know how?

    Posted 10 years ago #
  7. *puzzled* .. you have a single.css which should be loaded for a single post and you have a page.css which should be loaded for a page?

    Then you should have something like this:


    function childtheme_css() {
    if (is_single()) {?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/single.css" /> <?php }
    if (is_page()) {?>
    <link rel="stylesheet" type="text/css" href="<?php echo bloginfo('stylesheet_directory') ?>/page.css" /> <?php }}
    add_action('wp_head', 'childtheme_css');

    Cheers,

    Chris

    Posted 10 years ago #
  8. Thanks Chris,

    it works ... !

    *currently i see your Blog the first Time and i must say "Hello" from beautiful Rhine :-)

    Posted 10 years ago #

RSS feed for this topic

Reply

You must log in to post.