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?

  • 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 – its_me Jun 30 '12 at 11:22
  • does your theme use body_class() in the body tag (in header.php ?) ? – Michael Jun 30 '12 at 12:50
  • Any progress on this (or your other) question(s)? – kaiser Nov 22 '14 at 9:12
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...

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.