11

Before I get started, I want to acknowledge that there are some really good articles that are targeted at how to conditionally enqueue scripts based on page/post content.

These are great, but they're much more advanced than what I want to do.

I feel like the answer is using the built-in is_page() function (codex), but when I try to use it the site breaks, or just doesn't work.

I think I'm just executing the conditional logic in the wrong spot.

Here's what I've tried to add to my functions.php:

function wpse39130_register_more_stylesheets() {
    wp_register_style( 'stylesheet_name', get_stylesheet_directory_uri() . '/stylesheet.css' );
}
add_action( 'init', 'wpse39130_register_more_stylesheets' );

function wpse39130_conditionally_enqueue_my_stylesheet() {
    // only enqueue on product-services page slug
    if ( is_page( 'products-services' ) ) {
        wp_enqueue_style( 'stylesheet_name' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse39130_conditionally_enqueue_my_stylesheet' );

When I remove the conditional part of it, the stylesheet is successfully enqueued so I know that works.

  • Actually i also used copy and paste your code and its work perfectlystrong text – abhishek abhi Abhi Feb 13 '17 at 9:04
4

I copy pasted your code into my dev environment, changed nothing but the page name, and it works just fine. Are you sure that it's not being enqueued and you just have it pointed wrong or that the page is not named incorrectly or something?

| improve this answer | |
  • 1
    Thanks, it does work! I don't know why it wasn't working before. Stranger things have happened I guess. Hopefully this will be of use to someone else. – Evan Mattson Jan 17 '12 at 0:19
2

What you've got is right--you're just hooking it to the wrong action. Try the "wp" action instead of "init". When init runs, the is_page() won't work properly yet.

EDIT: I'm wrong. I thought you had add_my_stylesheet hooked to init, but you don't. is_page should work from the enqueue_scripts action. Sorry...carry on...

| improve this answer | |
-1

I was trying to follow the same code and see if I can get same result, what I noticed is_page() doesn't work as expected. so I followed global variable $post and checked the category name/slug and latter use simple text compare to decide the action.

    global $post;
    $postcat = get_the_category( $post->ID );
if ( ! empty( $postcat ) ) {
 echo esc_html( $postcat[0]->ID );   // Debug 
 echo esc_html( $postcat[1]->name ); // Debug 
 echo var_dump($postcat ); // Debug 
}

The value of category if its just one category (not subcategory ) would be

echo esc_html( $postcat[0]->name ); //Display name on screen

For Sub category you will have following variable filled in

echo esc_html( $postcat[1]->name ); //Display name on screen

Now based upon your need use the code below, like I am using to check subcategory name:

//Use $postcat[0]->name for parent category name Can be your sub category name 
        if ( $postcat[1]->name == 'Shopping' ) { 

                wp_enqueue_style( 'stylesheet_name' );
            }else{
        // other code 
        }

Full Code:

        function wpse39130_register_more_stylesheets() {
        wp_register_style( 'stylesheet_name', get_stylesheet_directory_uri() . '/whatsqshop.css' );
    }
    add_action( 'init', 'wpse39130_register_more_stylesheets' );

    function wpse39130_conditionally_enqueue_my_stylesheet() {
        // only enqueue on product-services page slug
    global $post; //GLobal Post variable 
    $postcat = get_the_category( $post->ID ); // Get the Category info from POST ID

    if ( ! empty( $postcat ) ) { // IF POST CATEGORY IS NOT EMPTY
    //    echo esc_html( $postcat[0]->slug );   //Display SLUG On output screen <Debug option>
  //echo esc_html( $postcat[0]->name ); //Display name on screen
     //echo esc_html( $postcat[1]->name ); //Display name on screen

    //echo var_dump($postcat ); // For debug 
    }
    //  if( is_single(88)) {
       if ( $postcat[1]->name == 'Shopping' ) {
            wp_enqueue_style( 'stylesheet_name' );
        }else{
    //wp_enqueue_style( 'stylesheet_name' );
    }

    }
    add_action( 'wp_enqueue_scripts', 'wpse39130_conditionally_enqueue_my_stylesheet' );

Example: *No Extra CSS http://whatsq.com/category/gst16/

*Extra CSS with Background from the above category only http://whatsq.com/information-technology/shopping-information-technology/mothers-day-gift-plan-show-mom-the-love-with-flowers-and-and-more/

| improve this answer | |
  • Did I ask a question ? Its just my answer to the current topic . Which is more in detail. – Piclaunch S May 7 '18 at 21:06

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.