【问题标题】:How to Enqueue the Bootstrap Script and Style for WordPress Page Template but not Whole Site如何将 WordPress 页面模板而非整个站点的引导脚本和样式排入队列
【发布时间】:2018-10-14 21:18:39
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    使用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
    
    }
    

    提供的链接包含您正在寻找的确切示例。

    【讨论】:

    • 基本问题...我应该为'yourpagename' 输入什么?我的模板文件是course-manager.php,但是当我按照您的建议包装整个脚本并放入is_page('course-manager')) 时,它破坏了引导脚本,并且站点的其余部分仍然被破坏。两全其美......
    • 改了答案,
    • 完美,我已经为此努力了好几天。谢谢!
    • 发生了。很高兴能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2016-08-13
    • 2017-03-17
    • 1970-01-01
    相关资源
    最近更新 更多