0

I'm using woothemes Canvas with a child theme. Additionally, I am using the WP Listings plugin. I am creating a custom listings page template (overriding the plugin template) and I'm having issues recognizing the page type to include scripts/styles on that page type only.

Example:

if ( is_page_template( 'single-listing.php' ) ) {
    add_action('wp_enqueue_scripts', 'your_function_name');
    function your_function_name() {

the if statement is returning false, although it is using that file.

  • 1
    Hey Due to certain global variables being overwritten during The Loop is_page_template() will not work. In order to use it after The Loop you must call wp_reset_query() after The Loop. Please use wp_reset_query() if is_page_template call within the loop – Amit Mishra Dec 10 '14 at 20:25
3

I think your problem is how your function is constructed and not your condition as such.

You should not be wrapping your function and your action in a condition like this. Page templates are selected really late in the query by the main query, and I probably think that this is way to late for your action to execute. By the time the condition hits true, the wp_enqueue_scripts hook already executed and cannot be rerun

If this is a single page, and that single page is for a custom post type called listing, you should be using is_singular( 'listing' ) instead of is_page_template()

The correct way would be to wrap your scripts and styles inside your function inside your condition

You can try the following

add_action( 'wp_enqueue_scripts', 'your_function_name' );
function your_function_name() {
    if( is_singular( 'listing' ) {

        // add your scripts and styles

    }
}

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.