Semi-new to WordPress, so apologies is this is an obvious question!
I’m trying to execute some JavaScript on a WordPress site, but only on the child pages of a specific parent page – so, I have example.com/books, and I want the script to run on example.com/books/foo, example.com/books/bar, example.com/books/wee, and so forth. I don’t want it to run on example.com/books.
I know that I could just add in the script on each individual page, but I have other people editing the site who may add new book child pages, and they will not feel comfortable adding in Javascript.
Any solutions?
2 Answers
You don’t quite cover how far you got and if you are familiar with basic of WP enqueue process.
Focusing on parent part I the basic code would go like this:
add_action( 'wp_enqueue_scripts', function () {
if ( is_page() && in_array( $book_page_id, get_post_ancestors( get_the_id() ) ) ) {
// enqueue stuff
}
} );
In your examples you only have one level and might have worked to just check post_parent
of current post, but checking for ancestors is more thorough for more nested cases.