使用WordPress\'conditional tags, 全局变量、定义、类检查等。例如:
<?php
// load helper functions - always
require_once get_stylesheet_directory() . \'/inc/helper-functions.php\';
// load admin functions - for back-end only
if ( is_admin() )
require_once get_stylesheet_directory() . \'/inc/admin-functions.php\';
// load WooCommerce functions - when WooCommerce is active
if ( in_array(
\'woocommerce/woocommerce.php\',
apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) )
)
require_once get_stylesheet_directory() . \'/inc/woocommerce-functions.php\';
// load SomeClass\' functions - when class SomeClass is known
if ( class_exists( \'SomeClass\' ) )
require_once get_stylesheet_directory() . \'/inc/someclass-functions.php\';
// load debug functions - when debugging
if ( defined( \'WP_DEBUG\' ) && WP_DEBUG )
require_once get_stylesheet_directory() . \'/inc/debug-functions.php\';
您还可以在满足某些功能内的某些条件时退出:
add_action( \'wp_enqueue_scripts\', \'wpdev_143439_enqueue_scripts\' );
function wpdev_143439_enqueue_scripts() {
if (
! is_single()
|| is_singular( \'my_cpt\' )
|| (
defined( \'NEVERSCRIPT\' )
&& NEVERSCRIPT
)
) {
return;
}
// Enqueue some scripts here
}