gpt4 book ai didi

php - 如何根据模板部分对脚本进行排队?

转载 作者:搜寻专家 更新时间:2023-10-31 21:08:17 26 4
gpt4 key购买 nike

希望能够根据wordpress get template part在head and foot中加载脚本和stylesheet

我在我的 front-page.php 中有以下代码:

<?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();

现在在我的函数 php 中我想这样做

// 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' );
}
}
}

然而,脚本是为我的第一个条件加载的,但如果不是,则为其他条件加载?为什么会这样?

最佳答案

如果您将字符串或变量传递给它,则 if 会测试有形的东西。例如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();

关于php - 如何根据模板部分对脚本进行排队?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28332354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com