很明显,你可以像以前那样做。然而,有一种更好的方法,比以这种方式在页脚中添加脚本标记更易于维护。
您可以使用wp_localize_script()
作用
例如,在functions.php
:
function theme_wp_footer_scripts() {
/* your custom CODE */
if ( $body_logged_in == \'on\' ) {
$count = 60;
} else {
$count = 90;
}
if ( $navbar_sticky == \'on\' ) {
$count = $count + 45;
}
/* add main script in footer */
wp_enqueue_script( \'footer_script\', get_stylesheet_directory_uri() . \'/js/my-script.js\', array( \'jquery\' ), null, true );
/* add dynamic data for your footer_script with object name footer_script_data */
wp_localize_script( \'footer_script\', \'footer_script_data\',
array(
\'count\' => $count
)
);
}
add_action( \'wp_enqueue_scripts\', \'theme_wp_footer_scripts\' );
然后在
js/my-script.js
:
jQuery(document).ready(function($) {
/* Your custom JavaScript CODE that is jQuery dependent */
/* see how footer_script_data object name is used from external JavaScript file */
$(\'.fixed-sidebar\').stick_in_parent({offset_top: footer_script_data.count});
});
更多关于
wp_localize_script.