Check order of the stylesheet loading in the header.
Emphasize the selector class name or id by adding something, for example: if you want the change the body background do not just put "body{background: url("body-background1.jpg");}" add a word to the body selector. Declare the class name like this "head + body{background: url("body-background1.jpg");}". I do not know the reason why but it works.
1
4
|
I'm using the well-written Oenology theme by Chip Bennett as the Parent to my own Child theme.
In my development process, I've discovered that there are some challenges for people writing Child themes when it comes to controlling styles.
I've just discovered that my main style.css file gets loaded before every other stylesheet link or statement in <head>, and this explains why I was having trouble overriding some of the Parent styles.
further study of the problem shows that various Parent stylesheets and styles can be queued in the <head> in three places;
add_action('wp_print_styles', , add_action('wp_enqueue_scripts', , and then add_action('wp_head', .
to keep things simple, I'm planning to create two stylesheets. the first main 'style.css' sheet would only include the
@import url() command, needed to load Oenology's main stylesheet.
the second stylesheet would contain my Child rules. to make sure that it's loaded after all of the other rules, I'd queue it using
add_action( 'wp_head', .
does this sound reasonable? or is there a better (more correct) way to do it?
Update
wp_enqueue_style() doesn't appear to work in wp_head().
cheers,
Gregory | |||
add comment |
7
|
Just FYI, this question probably borders on too localized, as it is specific to the Oenology Theme.
That said, here's where I think you're having a problem: Oenology enqueues two style sheets:
The varietal stylesheet, which applies the "skin", enqueues at priority
11 , to ensure that the base stylesheet, style.css , loads first, and the varietal stylesheet loads second, in order to cause the correct cascade.
So, if you need to override the varietal stylesheet, simply enqueue your second stylesheet afterthe varietal stylesheet; i.e. with a priority of at least
12 or greater.Edit
To provide a more-general answer, to the more-general question:
In order to override a Parent Theme stylesheet enqueue, you need to know two things:
Enqueue functions (
wp_enqueue_script() /wp_enqueue_style() ) can be properly executed anywhere between the init hook and the wp_print_scripts /wp_print_styles hooks. (Thesemantically correct hook at which to execute wp_enqueue_*() functions is currently wp_enqueue_scripts .) This list includes the following actions (among others; these are just the usual suspects):
(Note that
wp_enqueue_scripts , wp_print_styles , and wp_print_scripts all fire inside of wp_head , at a specific priority.
So, in order to override a Parent-Theme stylesheet, you need to do one of the following:
| ||||||||||||
|
1
|
You should always use
wp_enqueue_style() to load your stylesheet and that functions should be hooked to the wp_enqueue_scripts hook with runs in the head. I suspect that you weren't hooking it there, hence your problem. (As of WP 3.3, if wp_enqueue_style() is hooked to anything else, it throws a Notice with WP_Debug turned on). | ||||||||
|
-1
|
WP needs a better style enqueuing system. every style, whether a file link or an inline style, should be passed through the same hook allowing the Priority to determine their order. optionally for debugging purposes, it would be useful if the hook also output the Priority values of each style perhaps in commented form.
Update
I have been able to retain control of the styles in my Child theme while maintaining the original cascade of the Parent theme (which uses a main stylesheet, a sub-stylesheet, and a few style statements) by splitting my stylesheet into two as explained in the question. I am no longer using the wp_head() hook, instead using the proper and standard wp_enqueue_scripts() hook with a high ordinal to ensure that my stylesheet gets loaded last.
my Child theme's default stylesheet:
the function to enqueue my main stylesheet:
there are however a few WP-generated style statements (i.e., not the parent theme) that are stated after my stylesheet, and I'll have to look at using either high specificity css rules to override the rules that concern me, or look for hooks to disable them, in particular the body.custom-background rule.
thank you all for your comments.
cheers,
Gregory |
Nice knowledge gaining article. This post is really the best on this valuable topic. autoblog wordpress website
ReplyDelete