0

I am trying figure out how to place JavasScript and stylesheet to parent page and children page via functions.php. The parent page is Our People and children page is staff names.

This is what I wrote:

JavaScript

wp_register_script ('unslider-js', get_stylesheet_directory_uri() . '/library/js/unslider-min.js', array( 'jquery' ),'1',false);

if ( is_page('our-people') && in_array( get_post_ancestors(597) ) ) { wp_enqueue_script ('unslider-js'); }

Stylesheet

wp_register_style ('unslider-css', get_stylesheet_directory_uri() . '/library/css/unslider.css', array(),'', 'all');
if ( is_page() && in_array( get_post_ancestors(597) ) ) { wp_enqueue_style ('unslider-css'); }

It seem that it does not fetch these files for the parent and child page. Could anyone guide me how to achieve this approach?

1 Answer 1

0

This code will check to make sure that the Our People page is being viewed (page with slug our-people) or one of the children of page with ID 597, and will enqueue both the JavaScript and CSS if so:

function wpse241030_scripts() {
    // Bail if we're not looking at the Our People page or one of the children of page with ID 597.
    if ( ! is_page( 'our-people' ) && ! in_array( 597, (array) get_post_ancestors( get_the_ID() ) ) ) {
        return;
    }

    // Enqueue unslider JS
    wp_enqueue_script( 'unslider-js', get_stylesheet_directory_uri() . '/library/js/unslider-min.js', array( 'jquery' ), '1', false );

    // Enqueue unslider CSS
    wp_enqueue_style( 'unslider-css', get_stylesheet_directory_uri() . '/library/css/unslider.css', array(), '', 'all' );
}
add_action( 'wp_enqueue_scripts', 'wpse241030_scripts' );
2
  • Thank you for the answer Dave, but when I copied your codes to functions.php and refresh the our-people page or our-people/person-name does not fetch the files required. Whats the correct step to make your code work? Commented Oct 1, 2016 at 1:50
  • Sorry it's not working for you, William. I've just triple checked the code, and it's working for me; the unslider-min.js and unslider.css files files are enqueued on (and only on) the our-people page or its sub-pages. Commented Oct 2, 2016 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.