0

I'm trying to change the stylesheet for specific page/post while viewing in frontend, but I can't find the hook or filter for doing that ..

I've tried many hooks and pages and it doesn't work

Should I use page_css_class ?

apply_filters( 'page_css_class', array $css_class, WP_Post $page, int $depth, array $args, int $current_page )
1
  • 1
    Can you try to post and explain what have you tried and what "it doesn't work" means? Otherwise we can not know if using page_css_class filter is the solution or not. Hint: try to ask about your problem, not about what you think is the solution.
    – cybmeta
    Commented Sep 5, 2016 at 9:04

2 Answers 2

1

The filter page_css_class allows you to change the classes on all pages, when they are listed. This is not what you are looking for.

In stead, you'll want to enqueue different style sheets for different posts/pages. This you can do using conditionals. Include a function like this in your functions.php (example will load a special style for the page with slug "about-us"):

function wpse238235_conditional_load_style() {
  if (is_page('about-us')) {
    wp_register_style('wpse238235-about-us-style', get_template_directory_uri() . '/about-us-style.css');
    wp_enqueue_style('wpse238235-about-us-style');
    }
 else {
    wp_register_style('wpse238235-default-style', get_template_directory_uri() . '/default-style.css');
    wp_enqueue_style('wpse238235-default-style');
    }

Beware that this assumes the mandatory style.css is also loaded somewhere.

5
  • By the way, if you just want minor changes in your style per post/page, you can also use the bodyclasses attached to every post/page and include the variations in your main stylesheet.
    – cjbj
    Commented Sep 5, 2016 at 9:21
  • body classes or page classes, both can be used. In the second opiton, page_css_class filter can be used to add custom classes if needed. So, page_css_class filter can be what the OP needs. I think we can not know that with the description of the question.
    – cybmeta
    Commented Sep 5, 2016 at 9:27
  • i want to hook style of every page/post by ID and by condition every page/post will have different style .. Commented Sep 5, 2016 at 9:27
  • @KarimMamdouh The page ID is included in the bodyclass on every page. You can use that to give different styles to every page from one stylesheet. If you want to include separate stylesheets for every ID you can use my answer.
    – cjbj
    Commented Sep 5, 2016 at 9:32
  • how i'll get post/page view hook ? Commented Sep 5, 2016 at 12:24
0

Look around for "child theme" of the theme you are using. Create something new. The basics for child themes is here

2
  • i want to hook style of every page/post by ID and by condition every page/post will have different style .. Commented Sep 5, 2016 at 9:11
  • Where will the style come from? Are you creating a separate CSS file for each? I'm not sure I understand. Commented Sep 5, 2016 at 17:39

Your Answer

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

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