twitter-bootstrap 使用functions.php将Bootstrap添加到Wordpress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36737453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 23:51:23  来源:igfitidea点击:

Adding Bootstrap to Wordpress Using functions.php

phpwordpresstwitter-bootstrap

提问by Kuber Kanav

I have tried using the following code to embed Bootstrap to Wordpress but it doesn't work. Need help..........

我曾尝试使用以下代码将 Bootstrap 嵌入 Wordpress,但它不起作用。需要帮忙..........

<?php  

 function resources() {

 wp_enqueue_style('style',get_stylesheet_uri());          
 wp_enqueue_style('bootstrap.min',get_template_directory_uri().'/css/bootstrap.min.css');
 wp_enqueue_style('bootstrap',get_template_directory_uri().'/css/bootstrap.css');
 wp_enqueue_style('bootstrap-theme.min',get_template_directory_uri().'/css/bootstrap-theme.min.css');
}     

add_action('wp_enqueue_scripts', 'resources');

回答by naveenkumar.s

This may be helpful to you

这可能对你有帮助

WordPress is to use wp_enqueue_style and wp_enqueue_script from within functions.php. In particular, we need to create a new function that adds (or enqueues) the css style and the js script and then allow WordPress to call it at the right moment by adding a wp_enqueue_scripts action.

WordPress 将在functions.php 中使用wp_enqueue_style 和wp_enqueue_script。特别是,我们需要创建一个新函数来添加(或入队)css 样式和 js 脚本,然后通过添加 wp_enqueue_scripts 操作允许 WordPress 在适当的时候调用它。

/* Add bootstrap support to the Wordpress theme*/

function theme_add_bootstrap() {
wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/css/bootstrap.min.css' );
wp_enqueue_style( 'style-css', get_template_directory_uri() . '/style.css' );
wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/js/bootstrap.min.js', array(), '3.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_add_bootstrap' );

For Reference: click here

供参考:点击这里

回答by Tri Th?c Nguy?n

In order towp_enqueue_scriptsin function.phpwork properly, <?php wp_head(); ?>needs to be placed before the closing </head>and <?php wp_footer(); ?>before closing </body>tag in your template file.

为了wp_enqueue_scriptsfunction.php正常工作,<?php wp_head(); ?>需要闭幕前放置</head><?php wp_footer(); ?>关闭之前</body>在模板文件中的标记。

Because you didn't post your template file, so I think this can be a reason causes your problem.

因为您没有发布模板文件,所以我认为这可能是导致您出现问题的一个原因。

Hope this help

希望这有帮助

回答by Astrophe

Just use the bootstrap CDN - https://www.bootstrapcdn.com/

只需使用引导 CDN - https://www.bootstrapcdn.com/

In your child theme find the function theme_enqueue_styles() and add the two lines of extra code shown below.

在您的子主题中找到函数 theme_enqueue_styles() 并添加如下所示的两行额外代码。

function theme_enqueue_styles() {
    // Add the following two lines //
    wp_enqueue_style( 'bootstrap-cdn-css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );
    wp_enqueue_script( 'bootstrap-cdn-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' );
    // ------               -------//
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') );
}

回答by YASH THUMAR

Make sure your file location of downloaded bootstrap files. and put this code into functions.php

确保您下载的引导程序文件的文件位置。并将此代码放入functions.php

1.stylesheet CSS file

1.stylesheet CSS文件

2.script js file

2.脚本js文件

<?php

function load_stylesheets()

{

// enqueue parent styles

wp_enqueue_style('bootstrap', get_stylesheet_directory_uri().' /css/bootstrap.min.css');

wp_enqueue_script('bootstrap', get_stylesheet_directory_uri().' /js/bootstrap.min.js');

}

add_action('wp_enqueue_scripts','load_stylesheets');

?>