本文介绍了如何为WordPress页面模板而不是整个网站加入引导脚本和样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WordPress的新手,正在为我的网站构建一个页面模板。我想保留大部分站点的主要WordPress主题,但是我有一个非常特定的应用程序,我希望仅在几个页面上使用Bootstrap。为了避免主题更新时丢失自定义工作,我用页面模板制作了一个子主题,然后从WordPress中为相应页面选择了该模板。

I am new to WordPress and am building a single page template for my site. I want to keep my main WordPress theme for the majority of the site, but I have a very specific application that I want to use Bootstrap for on just a couple of pages. To avoid losing my custom work when the theme updates, I made a child theme with the page template and then selected that template from WordPress for the appropriate page.

我终于到了在我的 functions.php 的此脚本中(基于示例):

I finally arrived at this script for my functions.php (based on example 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尝试加载我的页面模板时才应用此方法,而在其他所有情况下忽略它?

That worked perfect for my page template, it now responds to Bootstrap classes... unfortunately, so does everything else on my site. How do I make this only apply if Wordpress is trying to load my page template, but ignore it in all other cases?

推荐答案

使用

if (is_page_template($templatename))
{
wp_enqueue_script('bootstrap-js', get_stylesheet_directory_uri().'/bootstrap/js/bootstrap.min.js', array('jquery'), NULL, true);

...etc

}

提供的链接包含您要查找的准确示例。

The link provided has an exact example you are looking for.

这篇关于如何为WordPress页面模板而不是整个网站加入引导脚本和样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:04