Skip to main content
clean up formatting, namespace functions for copy/paste friendliness
Source Link
Evan Mattson
  • 956
  • 1
  • 10
  • 20

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 register_more_stylesheetswpse39130_register_more_stylesheets() {
    wp_register_style( 'stylesheet_name', get_stylesheet_directory_uri() . '/stylesheet.css' );
}
add_action( 'init', 'wpse39130_register_more_stylesheets' );

function add_my_stylesheetwpse39130_conditionally_enqueue_my_stylesheet() {
    if (// is_page('products-services')only )enqueue //on usingproduct-services page slug
    wp_enqueue_styleif ( 'stylesheet_name'is_page( );'products-services' ) //) no{
 brackets needed for one line and no else
}
add_actionwp_enqueue_style( 'init', 'register_more_stylesheets''stylesheet_name' ); // should I 
 use wp_print_styles hook instead?}
}
add_action( 'wp_enqueue_scripts', 'add_my_stylesheet''wpse39130_conditionally_enqueue_my_stylesheet' );

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

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 register_more_stylesheets() {
    wp_register_style( 'stylesheet_name', get_stylesheet_directory_uri() . '/stylesheet.css' );
}
function add_my_stylesheet() {
    if ( is_page('products-services') ) // using page slug
    wp_enqueue_style( 'stylesheet_name' );  // no brackets needed for one line and no else
}
add_action( 'init', 'register_more_stylesheets' ); // should I use wp_print_styles hook instead?
add_action( 'wp_enqueue_scripts', 'add_my_stylesheet' );

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

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.

Tweeted twitter.com/StackWordPress/status/831074759001722881
Source Link
Evan Mattson
  • 956
  • 1
  • 10
  • 20

How to conditionally enqueue a stylesheet only for a certain page(s)?

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 register_more_stylesheets() {
    wp_register_style( 'stylesheet_name', get_stylesheet_directory_uri() . '/stylesheet.css' );
}
function add_my_stylesheet() {
    if ( is_page('products-services') ) // using page slug
    wp_enqueue_style( 'stylesheet_name' );  // no brackets needed for one line and no else
}
add_action( 'init', 'register_more_stylesheets' ); // should I use wp_print_styles hook instead?
add_action( 'wp_enqueue_scripts', 'add_my_stylesheet' );

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