WordPress.org

Forums

Creating a child theme when styles are loaded by functions.php (9 posts)

  1. bazqux
    Member
    Posted 2 years ago #

    I am trying to create a child theme for wp-creativix. I realize that the standard way of doing this is by creating a style.css in the child theme directory. The trouble is that there are two stylesheets for this theme (style.php and style.css) and most of the styles are in style.php. Further, the stylesheets are loaded by a function in functions.php. One idea I had was to add my own styles by creating a functions.php in the child theme with:

    add_action('wp_print_styles', 'bazqux_add_stylesheet');
    
    function bazqux_add_stylesheet() {
        $bazqux_style = get_stylesheet_uri();
        wp_register_style('bazqux_style', $bazqux_style);
        wp_enqueue_style('bazqux_style');
    
    }

    This does add my stylesheet, but it adds my stylesheet before the wp-creativix stylesheets. I'd like to add my stylesheet immediately after the parent theme's are loaded so my styles override the parent's. What's the best way to do this?

    Thank you.

  2. peredur
    Member
    Posted 2 years ago #

    If your (parent) theme has been well written, its functions will be surrounded by:

    if (!function_exists(')) { }

    See http://codex.wordpress.org/Child_Themes ('Using functions.php')

    If that's the case with your parent theme, then you can just override it and your own style sheet will be loaded instead of the parent's. You'll need to @import the parent's style sheet.

    If that's not the case in your parent theme, you could perhaps add it. Not ideal, since it changes the parent, but needs must when the devil rides, as they say.

    I don't know why themes get written in this way. I'm sure there must be a good reason, but I can't think of one, offhand.

    Cheers

    PAE

  3. bazqux
    Member
    Posted 2 years ago #

    Unfortunately the theme is not written that way. I figured out a solution though. You can pass a priority to the add_action method. 10 is the default. Passing in a number greater than 10 will cause the stylesheet to get loaded later. So I changed the add_action line to

    add_action('wp_print_styles', 'bazqux_add_stylesheet', 20);

    That works.

  4. halfon
    Member
    Posted 2 years ago #

    Thanks to a number of helpful forum posts and members, I have learned that the WP-Creativix theme is not very friendly for child themes. However, I would like to give it one more try using the above fix but it has not worked for me. Any suggestions?

    I incorporated the above code into my functions.php child theme file (nothing else in that file). But the code just appears at the top of my page?

    What am I missing?

    http://www.aluralegal.com

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    a new functions.php has to start with

    <?php

    in the first line; nothing whatsoever before that...

  6. halfon
    Member
    Posted 2 years ago #

    Thanks. That removed the code from my page but the changes I made to the style.php file still do not show up (i.e. they still do not override the style.php file in the parent).

    I did notice that the code above (which I copied directly) refers to "bazqux" which is obviously specific to the user above. However, I do not know how to modify for my page. Is that a reference to the child theme name or something else?

  7. alchymyth
    Forum Moderator
    Posted 2 years ago #

    your child theme's stylesheet style.css gets enqueued after the style.php of wp-creativex;

    lines 18 to 20 from the head section (first line is from the parent theme; last line is your stylesheet):

    <link rel='stylesheet' id='wpcx_sub_style-css'  href='http://www.aluralegal.com/wp-content/themes/wp-creativix/style.php?color=8eb03f&ver=3.5.1' type='text/css' media='all' />
    <link rel='stylesheet' id='sdsslider_main-css'  href='http://www.aluralegal.com/wp-content/plugins/smooth-dynamic-slider/css/frontslider.css?ver=3.5.1' type='text/css' media='all' />
    <link rel='stylesheet' id='bazqux_style-css'  href='http://www.aluralegal.com/wp-content/themes/wp-creativix-child/style.css?ver=3.5.1' type='text/css' media='all' />

    but the changes I made to the style.php file still do not show up

    there is no style.php of the child theme enqueued.
    'bazqux's code calls style.css - http://codex.wordpress.org/Function_Reference/get_stylesheet_uri

    you should be able to make the changes in style.css of the child theme.

  8. halfon
    Member
    Posted 2 years ago #

    Thank you! That worked. Very rewarding after nearly giving up on this problem. However, I am still a little confused.

    My child theme function.php file reads as follows in total:

    <?php
    
    add_action('wp_print_styles', 'bazqux_add_stylesheet', 20);
    
    function bazqux_add_stylesheet() {
        $bazqux_style = get_stylesheet_uri();
        wp_register_style('bazqux_style', $bazqux_style);
        wp_enqueue_style('bazqux_style');
    
    }

    I do not understand what the "bazqux" codes refer to as I do not see it anywhere in my files. I thought that was specific to the name of the user/member above. If so, could this be a problem in the future?

    Also, I was not totally clear about your reference to lines 18-20. The only link rel code I see is line nine of the header.php file.

    Just trying to understand for future reference.

  9. alchymyth
    Forum Moderator
    Posted 2 years ago #

    "bazqux"

    this is just a naming idea by the previous poster; should be no problem to keep;

    or you can rename you whole code if you like; example:

    <?php
    
    add_action('wp_print_styles', 'creativixchild_add_stylesheet', 20);
    
    function creativixchild_add_stylesheet() {
        $creativixchild_style = get_stylesheet_uri();
        wp_register_style('creativixchild_style', $creativixchild_style);
        wp_enqueue_style('creativixchild_style');
    
    }

    Also, I was not totally clear about your reference to lines 18-20. The only link rel code I see is line nine of the header.php file.

    this refers to the rendered code which ends up in the browser;
    because the stylesheets are 'enqueued', they are not 'linked' in header.php, but added via code to the 'wp_head()' hook.

    http://codex.wordpress.org/Function_Reference/wp_enqueue_style

Topic Closed

This topic has been closed to new replies.

About this Topic