0

I am trying to load a CSS Stylesheet to ONLY the "about" page.

I think I am 99% correct but what is wrong is the plugins_url - to link to the theme folder where I keep my CSS can I just use simply:

function testimonial_style() {
  wp_enqueue_style( 'custom_tooltip_frontend_css', url('path-to-my.css') );
} 

if(is_page( 42 )){
    add_action('wp_enqueue_scripts', 'testimonial_style');
}

Is it ok that I reference it with add_action and then "scripts" which is is is really a style and not a script...

2

Try this: I hope, It will works.

Explaination:

In below code in "is_page( 42 )" where 42 is page id of about page. So, if about page's id will be 42 then it's enqueue the stylesheet for that page only.

Also I define particular path for this CSS file by using wordpress function get_template_directory_uri().

function testimonial_style() {
  if ( is_page( 42 ) ) {
    wp_enqueue_style( 'custom_tooltip_frontend_css', get_template_directory_uri().'/path-to-my.css' ); 
  }
} 

add_action('wp_enqueue_scripts', 'testimonial_style');

Let me know How it help you.

2
  • Can you explain (by editing the answer) what you did and why you changed certain parts? So henry can not just copy your code but understand the logic behind it – kero Nov 6 '19 at 9:55
  • @kero This one is edited. – Nitesh Moree Nov 6 '19 at 10:38
0

Your code is ok. If it doesn't work, there's probably something wrong with your hooking order.

If you look at the list of hooks, you will see that there is no wp_enqueue_styles hook. So, yes, it is the wp_enqueue_scripts hook that you are supposed to add your action to. (There is a wp_enqueue_styles function, but that is something different, read more about how hooks work here).

In your code it is not clear where the line if(is_page( 42 )){ ... } resides. Normally you would put it in your theme's functions.php in a function that is called with the after_setup_theme hook. Looking again at the list of hooks you will see that after_setup_theme comes before wp_enqueue_scripts. So, at this point you can still attach functions to the latter hook.

Something in your question suggests that your are executing if(is_page( 42 )){ ... } in a plugin. That would still be fine (*) if it is in a function that hooks onto init. However, if you hook into wp_head you would be too late, because at that point functions hooked to wp_enqueue_scripts have already been executed.

(*) but if the plugin is executed while the theme is inactive you will get an error

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.