我是 WordPress 新手,正在为我的网站构建单页面模板。我想在网站的大部分内容中保留我的主要 WordPress 主题,但我有一个非常具体的应用程序,我想在几个页面上使用 Bootstrap。为了避免在主题更新时丢失我的自定义工作,我使用页面模板创建了一个子主题,然后从 WordPress 中为适当的页面选择了该模板。
我终于为我的functions.php
找到了这个脚本(基于示例here):
<?php
function enqueue_child_theme_styles() {
//deregister the parent bootstrap style and script
wp_deregister_style( 'bootstrap' );
wp_deregister_script( 'bootstrap' );
//enqueue my child theme stylesheet
wp_enqueue_style( 'child-style', get_stylesheet_uri(), array('theme') );
//enqueue bootstrap in the child theme
wp_enqueue_script('bootstrap-js', get_stylesheet_directory_uri().'/bootstrap/js/bootstrap.min.js', array('jquery'), NULL, true);
wp_enqueue_style('bootstrap-css', get_stylesheet_directory_uri().'/bootstrap/css/bootstrap.min.css', false, NULL, 'all');
}
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
这非常适合我的页面模板,它现在响应 Bootstrap 类...不幸的是,我网站上的其他所有内容也是如此。如何使其仅在 WordPress 尝试加载我的页面模板时适用,而在所有其他情况下忽略它?
最佳答案
if (is_page_template($templatename))
{
wp_enqueue_script('bootstrap-js', get_stylesheet_directory_uri().'/bootstrap/js/bootstrap.min.js', array('jquery'), NULL, true);
...etc
}
提供的链接包含您正在寻找的确切示例。
关于wordpress - 如何将 WordPress 页面模板的 Bootstrap 脚本和样式排入队列,而不是整个站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52802380/