0

I've been using Wordpress for a while, and came to a point where I need to make css customizations, but only for a certain page. I know that one way to do this is to use conditional tags:

if ( is_page( 'mypage' )

And that's where I get stuck!

Are there any other ways of making css customizations, only for a certain page?

3
  • conditional tags to edit css — that's not very clear. But you can have a CSS file load only on a specific page, if that's what you want Commented Jun 30, 2012 at 11:22
  • does your theme use body_class() in the body tag (in header.php ?) ? Commented Jun 30, 2012 at 12:50
  • Any progress on this (or your other) question(s)? Commented Nov 22, 2014 at 9:12

2 Answers 2

4

If you have a lot of rules that you need to conditionally apply (there's no hard and fast rule, but, to make one up, maybe 10-15+ lines?) then @aahan's answer is the way to go. If you only have a few though, just work off body class.

In your header.php file (that's the common name at least) make sure the <body> has:

<body <?php body_class(); ?>>

Then you'll get lots of classes you can use to write conditional styles like

.page-123 .entry-content
.template-my-custom-template-php h1

etc...

0
3

You'd need something like this (to be added in functions.php):

add_action('wp_enqueue_scripts','wpse57014_register_script');
function wpse57014_register_script(){

    //Register and enqueue stylesheet only on this page
    if ( is_page( 'mypage' ) ) {
        wp_register_style( 'wpse57014_mypage_stylesheet', get_template_directory_uri().'/mypage-style.css' );
        wp_enqueue_style( 'wpse57014_mypage_stylesheet' );
    }

}

Where mypage is the slug of the page on which ONLY you want to load the stylesheet. Replace instances of mypage in the code to keep the code understandable to you (or modify it as you wish, if you know what you are doing).

get_template_directory_uri().'/mypage-style.css' is the path to the stylesheet in your theme, for example — http://example.com/wp-content/themes/MY THEME/mypage-style.css.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.