wordpress - 如何将 WordPress 页面模板的 Bootstrap 脚本和样式排入队列,而不是整个站点

标签 wordpress twitter-bootstrap wordpress-theming custom-wordpress-pages

我是 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 尝试加载我的页面模板时适用,而在所有其他情况下忽略它?

最佳答案

使用is_page_template

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/

相关文章:

javascript - 日期时间选择器显示在上方时不工作

html - CSS 页面在空白/边距上向右滚动和跨浏览器兼容性

javascript - 在 Javascript 中调用嵌套的 div 类。修改后的代码在 WordPress 中不起作用

javascript - jquery bootstrap datepicker点击事件只触发一次

html - 如何更改表单的输入背景颜色?

php - WooCommerce 函数 wc_get_template() 修改

php - wordpress 数据库调用成员错误

css - Bootstrap 3 导航栏折叠宽度

WordPress wpaautop 问题

wordpress - 将除一个之外的所有 WordPress 页面重定向到 SSL