12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on Jun 23, 2019

Conditionally targeting all Pages that use a Oxygen Template

Sridhar Katakam

In a PSD > WP project that I recently completed, the requirement was to implement Tocbot on all Pages that use a specific Oxygen Template.

With a traditional theme-based approach we can use is_page_template( 'page-example.php') to target all Pages that use a page template so that wp_enqueue_script() can be called inside a if conditional.

add_action( 'wp_enqueue_scripts', 'custom_enqueue_files' );
/**
 * Loads <list assets here>.
 */
function custom_enqueue_files() {

	if ( is_page_template( 'page-example.php' ) ) {
		wp_enqueue_script( 'tocbot', plugin_dir_url( __FILE__ ) . 'assets/js/tocbot.min.js', array(), '4.3.1', true );
	}

}

This is not possible with Oxygen but we can do a little out of the box thinking and achieve it like so:

  • add a body class to the Pages that use the Oxygen Template
  • loop through all the body classes, look for the above body class and then enqueue the script.

This members-only tutorial provides the details to implement the above.

Step 1

Edit the Oxygen Template.

Add a Code Block.

PHP:

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right