我希望能够根据wordpress get模板部分在头部和脚部加载脚本和样式表
<?php
/*
Template Name: Home Page
*/
get_header();
$loop = new WP_Query( array( 'post_type' => 'home_page','posts_per_page'=>1, 'orderby' => 'menu_order', 'order' => 'DESC') );
while ( $loop->have_posts() ) : $loop->the_post();
if( get_field('home_page_template_type') == 'Bookcase') {
get_template_part( 'bookcase' );
}elseif (get_field('home_page_template_type') == 'Bookers') {
get_template_part('bookers' );
}
endwhile;
get_footer();
// Register Script
if (!function_exists('theme_scripts')) {
function villarosa_theme_scripts() {
global $wp_scripts;
if ('bookcase.php') {
// Load our main stylesheet.
wp_deregister_style( 'bookcase-styles' );
wp_register_style( 'bookcase-styles',trailingslashit( THEME_URI ) .'bookcase/css/style.css', false, '1.0' );
wp_enqueue_style( 'bookcase-styles' );
// jQuery (necessary for Bootstrap's JavaScript plugins)
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', trailingslashit( THEME_URI ) .'bookcase/js/jquery.js', false, '1.0', true );
wp_enqueue_script( 'jquery' );
wp_deregister_script( 'jquery-migrate' );
wp_register_script( 'jquery-migrate', trailingslashit( THEME_URI ) .'bookcase/js/jquery-migrate-1.2.1.js', false, '1.0', true );
wp_enqueue_script( 'jquery-migrate' );
wp_deregister_script( 'jquery-easing' );
wp_register_script( 'jquery-easing', trailingslashit( THEME_URI ) .'bookcase/js/jquery.easing.1.3.js', false, '1.0', true );
wp_enqueue_script( 'jquery-easing' );
wp_deregister_script( 'script' );
wp_register_script( 'script', trailingslashit( THEME_URI ) .'bookcase/js/script.js', false, '1.0', true );
wp_enqueue_script( 'script' );
wp_deregister_script( 'jquery-equalheights' );
wp_register_script( 'jquery-equalheights', trailingslashit( THEME_URI ) .'bookcase/js/jquery.equalheights.js', false, '1.0', true );
wp_enqueue_script( 'jquery-equalheights' );
wp_deregister_script( 'jquery-ui-totop' );
wp_register_script( 'jquery-ui-totop', trailingslashit( THEME_URI ) .'bookcase/js/jquery.ui.totop.js', false, '1.0', true );
wp_enqueue_script( 'jquery-ui-totop' );
wp_deregister_script( 'superfish' );
wp_register_script( 'superfish', trailingslashit( THEME_URI ) .'bookcase/js/superfish.js', false, '1.0', true );
wp_enqueue_script( 'superfish' );
wp_deregister_script( 'jquery-mobilemenu' );
wp_register_script( 'jquery-mobilemenu', trailingslashit( THEME_URI ) .'bookcase/js/jquery.mobilemenu.js', false, '1.0', true );
wp_enqueue_script( 'jquery-mobilemenu' );
wp_deregister_script( 'sForm' );
wp_register_script( 'sForm', trailingslashit( THEME_URI ) .'bookcase/js/sForm.js', false, '1.0', true );
wp_enqueue_script( 'sForm' );
} else if ('bookers.php') {
// Load our main stylesheet.
wp_deregister_style( 'bookers-styles' );
wp_register_style( 'bookers-styles',trailingslashit( THEME_URI ) .'bookers/css/style.css', false, '1.0' );
wp_enqueue_style( 'bookers-styles' );
wp_deregister_style( 'stuck' );
wp_register_style( 'stuck',trailingslashit( THEME_URI ) .'bookers/css/stuck.css', false, '1.0' );
wp_enqueue_style( 'stuck' );
// jQuery (necessary for Bootstrap's JavaScript plugins)
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', trailingslashit( THEME_URI ) .'bookers/js/jquery.js', false, '1.0', true );
wp_enqueue_script( 'jquery' );
wp_deregister_script( 'jquery-migrate' );
wp_register_script( 'jquery-migrate', trailingslashit( THEME_URI ) .'bookers/js/jquery-migrate-1.2.1.js', false, '1.0', true );
wp_enqueue_script( 'jquery-migrate' );
wp_deregister_script( 'jquery-easing' );
wp_register_script( 'jquery-easing', trailingslashit( THEME_URI ) .'bookers/js/jquery.easing.1.3.js', false, '1.0', true );
wp_enqueue_script( 'jquery-easing' );
wp_deregister_script( 'script' );
wp_register_script( 'script', trailingslashit( THEME_URI ) .'bookers/js/script.js', false, '1.0', true );
wp_enqueue_script( 'script' );
wp_deregister_script( 'jquery-equalheights' );
wp_register_script( 'jquery-equalheights', trailingslashit( THEME_URI ) .'bookers/js/jquery.equalheights.js', false, '1.0', true );
wp_enqueue_script( 'jquery-equalheights' );
wp_deregister_script( 'jquery-ui-totop' );
wp_register_script( 'jquery-ui-totop', trailingslashit( THEME_URI ) .'bookers/js/jquery.ui.totop.js', false, '1.0', true );
wp_enqueue_script( 'jquery-ui-totop' );
wp_deregister_script( 'superfish' );
wp_register_script( 'superfish', trailingslashit( THEME_URI ) .'bookers/js/superfish.js', false, '1.0', true );
wp_enqueue_script( 'superfish' );
wp_deregister_script( 'tmStickUp' );
wp_register_script( 'tmStickUp', trailingslashit( THEME_URI ) .'bookers/js/tmStickUp.js', false, '1.0', true );
wp_enqueue_script( 'tmStickUp' );
wp_deregister_script( 'jquery-mobilemenu' );
wp_register_script( 'jquery-mobilemenu', trailingslashit( THEME_URI ) .'bookers/js/jquery.mobilemenu.js', false, '1.0', true );
wp_enqueue_script( 'jquery-mobilemenu' );
}
}
}
然而,脚本是为我的第一个条件加载,但如果它们不是?为什么会这样?
答案 0 :(得分:1)
如果你传递一个字符串或变量,测试一些有形的东西。例如if($var)
将等同于if($var != null)
,因此您的函数不会超过if ('bookcase.php')
,因为它始终为真(您不测试此文件是否存在,您正在测试它是否为空)
你想要的可能是:
function villarosa_theme_scripts($type) {
if($type==='bookcase'):
// enqueue scripts
elseif($type==='bookers':
// enqueue scripts
endif;
}
您可以直接在模板上调用它*将此代码放在标题之前......
if( get_field('home_page_template_type') == 'Bookcase') {
get_template_part( 'bookcase' );
villarosa_theme_scripts('bookcase');
}elseif (get_field('home_page_template_type') == 'Bookers') {
get_template_part('bookers' );
villarosa_theme_scripts('bookers');
}
get_header();
答案 1 :(得分:1)
我看不到你在哪里调用villarosa_theme_scripts,但无论如何:
if ('bookcase.php')
将始终评估为true。你需要测试一些变量(我假设它是一个WordPress模板名称),如:
if ($template == 'bookcase.php')