使用函数将引导程序添加到WordPress.php


Adding Bootstrap to Wordpress Using functions.php

我尝试使用以下代码将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');

这可能对您有所帮助

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

/* 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' );

供参考:点击这里

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

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

希望这个帮助

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

在子主题中找到函数 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') );
}

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

1.样式表CSS文件

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');
?>