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
    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 Sep 5 '16 at 9:04
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.

  • 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 Sep 5 '16 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 Sep 5 '16 at 9:27
  • i want to hook style of every page/post by ID and by condition every page/post will have different style .. – Karim Mamdouh Sep 5 '16 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 Sep 5 '16 at 9:32
  • how i'll get post/page view hook ? – Karim Mamdouh Sep 5 '16 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

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

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.