[英]Wordpress - load jQuery in head on specific page Template
我有一個頁面模板,需要在頁眉而不是頁腳中加載jQuery。
默認情況下,主題(Divi主題)將jQuery加載到頁腳中。 當jQuery加載兩次時,這會導致一些錯誤。
有什么辦法可以修改<?php wp_footer(); ?>
<?php wp_footer(); ?>
為該模板排除jQuery?
謝謝。
模板:
<!DOCTYPE html>
<html <?php language_attributes(); ?> class="no-js">
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<?php echo get_site_url(); ?>/cam-forms/styles/cam-forms.min.css" />
<script type="text/javascript" src="<?php echo get_site_url(); ?>/cam-forms/js/jquery.js"></script>
<script type="text/javascript" src="<?php echo get_site_url(); ?>/cam-forms/js/jquery-migrate.min.js"></script>
</head>
<body>
<?php echo do_shortcode( get_field('form') ); ?>
<?php wp_footer(); ?>
</body>
</html>
您必須添加<?php wp_head(); ?>
<?php wp_head(); ?>
在以下位置:
<!DOCTYPE html>
<html <?php language_attributes(); ?> class="no-js">
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<?php echo get_site_url(); ?>/cam-forms/styles/cam-forms.min.css" />
<script type="text/javascript" src="<?php echo get_site_url(); ?>/cam-forms/js/jquery.js"></script>
<script type="text/javascript" src="<?php echo get_site_url(); ?>/cam-forms/js/jquery-migrate.min.js"></script>
<?php wp_head(): ?>
</head>
<body>
<?php echo do_shortcode( get_field('form') ); ?>
<?php wp_footer(); ?>
</body>
</html>
然后,在您的functions.php文件中添加它。
function theme_scripts(){
wp_register_script('fileName', get_template_directory_uri() . '/js/file.js', array( ), '1.0.0');
wp_enqueue_script('fileName');
}
add_action('init', 'theme_scripts');
另外,我始終建議您使用CSS和JS的縮小文件。
在function.php中添加以下函數以在頁眉中添加js和css文件,並在頁腳中刪除文件
<?php
remove_action('wp_footer', 'wp_enqueue_scripts', 1);
add_action('wp_head', 'wp_enqueue_scripts', 5);
function mycustomscript_enqueue() {
wp_enqueue_style( 'ca-forms-min', get_template_directory_uri() . '/cam-forms/styles/cam-forms.min.css', array('ca-forms-min'));
wp_enqueue_script( 'jquery', get_stylesheet_directory_uri() . '/cam-forms/js/jquery.js', array( 'jquery' ));
wp_enqueue_script( 'jquery-migrate', get_stylesheet_directory_uri() . '/cam-forms/js/jquery-migrate.min.js', array( 'jquery-migrate' ));
}
add_action( 'wp_enqueue_scripts', 'mycustomscript_enqueue' );
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.