26 responses to “Contextually changing your theme’s stylesheet”

  1. Ptah Dunbar

    Ah, neat little trick. That’s something I’ve had to explain to several users when they were interested in trying to add a custom stylesheet for page templates etc. (even though a simple .page-template-archives rule would suffice).

  2. Jeremy

    Useful, simple to implement and well explained: another great tip of yours, thank you!

  3. Peter Kahoun

    Could you provide an example when replacing whole stylesheet is more “better” than adding some overriding rules?

  4. Adam W. Warner

    Can someone explain what the 10 and 2 are doing in this line?:

    add_filter( 'stylesheet_uri', 'my_stylesheet', 10, 2 );
  5. Ptah Dunbar

    @Adam – 10 is the priority that my_stylesheet will run. So that means it has to wait until everything less than 10 is run before this function gets executed. 2 is the number of arguments my_stylesheet accepts ($stylesheet_uri and $stylesheet_dir_uri).

  6. Adam W. Warner

    Thanks Ptah, now I fully understand what’s happening in the above code:)

  7. links for 2009-07-28 | Links | WereWP

    [...] Contextually changing your theme’s stylesheet A simple and yet really useful tutorial to help you using different styles for different WordPress pages. (tags: WordPress tutorial style css) [...]

  8. fb

    I would also agree with the comment here http://justintadlock.com/archives/2009/07/27/contextually-changing-your-themes-stylesheet#comment-143544

    As you have all of your themes so well coded, why do we need to have additional css files?

    We can just add over riding css rules.. as most of us cache the css files locally (on clients sites) why make them d/l a new one..

    Maybe I’m just barking up the wrong tree, Justin please enlighten me ( as you have thankfully done many times before)

    atb

  9. WordPress Watch » Blog Archive » How to use multiple stylesheets in one WordPress theme

    [...] Tadlock prefers to use the latter and has written a short tutorial on how to go about adding this functionality to your [...]

  10. fb

    Ok sure still seems a bit extreme, but understand that it could come in handy

    Thanks

  11. Peter Kahoun

    @Justin Got it now, thanks. Actually, you could go even more into extreme and load even entirely different theme… So the particular Page would look like a separated website. (Just thinking aloud…)

  12. fb

    Re: http://justintadlock.com/archives/2009/07/27/contextually-changing-your-themes-stylesheet#comment-143907

    loading a completely different theme, per page or post, this was (not sure if it still works) with all-in-one seo, you had the possibility of choosing what theme you wanted to use on a per-page/post basis, just my two cents…

  13. Robert Joe

    Indeed a great tip this, I really love you for this.

  14. WordPress Weekend Resources - July 31, 2009 | Theme Lab

    [...] Contextually changing your theme’s stylesheet – Justin Tadlock goes over how to dynamically change your theme stylesheet depending on which page you’re on through the functions.php file. Included in the post is all the code you’ll need, as well as an explanation. [...]

  15. Wordpress Blog Services - WordPress Weekend Resources - July 31, 2009

    [...] Contextually changing your theme’s stylesheet – Justin Tadlock goes over how to dynamically change your theme stylesheet depending on which page you’re on through the functions.php file. Included in the post is all the code you’ll need, as well as an explanation. [...]

  16. Chris

    For a real-world example, would this be a practical alternative to creating a separate home.php file, if I would like to remove the sidebar from my static front page?

  17. Wordpress Wednesday: 50 Links & Resources from around the web | Wordpress Guerrilla

    [...] Contextually changing your wordpress theme’s stylesheet [...]

  18. Modern Warfare 2

    Thanks Chris !! I was all messed up trying to use CSS :(

  19. rexsky

    hey Justin,great job.I just learn div and css last month..thanks for your sharing…learned a lot from your blog….again thanks…
    :)

  20. Dan Smart

    Thanks for the post. I’ve been looking for a solution like this, however I’m still trying to create a solution that doesn’t require you to have a list of pages in the function – it would be nice to be able to grab this from the custom page or something similar so that it’s totally extensible rather than dependent on you adding to the list of pages with is_page().

    Any thoughts?

  21. Sam Lehti

    This really is handy looking example. Never knew it was possible with WP itself. Have been using thesis to get things done but somedays want to use another theme and if it works with every theme I could use it instead of hacking every css file by hand.

  22. andri

    I need more tips about hybrid theme… thanks for this tips :)

  23. Scott Pelland

    Thanks, Justin. I’ve been looking for a solution just like this for some time. I also was wondering if you had given any thought to Dan’s question above?

  24. VB .Net Tutorial

    Thanks the idea of switching the styles as per the contxt is nice. Have you ever experienced issues with cache when switching themes.

    How about bypassing the cache. I always had problems if the different stylesheet is already in cache, then the theme wont be switched properly. I guess i need some sort of different names to store into cache.

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.