添加 Bootstrap css 到 wordpress 管理区而不添加 main style.css
Add Bootstrap css to wordpress admin area without adding main style.css
我正在创建一个新的自定义 Wordpress 主题,我正在尝试将 Boostrap CSS 添加到管理区域,但只有当我也将 style.css 添加到管理区域时它才有效. Boostrap js 正在添加到管理区域,但 CSS 没有,如何添加 Bootstrap CSS 而不将 style.css 添加到管理区域?
这是我的function.php
<?php
/*
* Function to register bootstrap css style
*/
function bootstrap_enqueue_styles() {
wp_register_style('bootstrap', get_template_directory_uri().'/bootstrap/css/bootstrap.min.css');
}
/*
* Function to register theme css style
*/
function main_enqueue_style(){
$dependencies = array('bootstrap');
wp_enqueue_style('main-css-style', get_stylesheet_uri(), $dependencies);
}
/*
* Function to register Bootstrap javascript
*/
function bootstrap_enqueue_scripts() {
$dependencies = array('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri().'/bootstrap/js/bootstrap.min.js', $dependencies, '3.3.6', true );
}
add_action( 'wp_enqueue_scripts', 'bootstrap_enqueue_styles' );
add_action( 'wp_enqueue_scripts', 'main_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'bootstrap_enqueue_scripts' );
add_action( 'admin_init', 'bootstrap_enqueue_styles' );
add_action( 'admin_init', 'bootstrap_enqueue_scripts' );
//add_action( 'admin_init', 'main_enqueue_style' ); <-- IF I DONT ADD THIS ONE IT DOESNT ADD BOOTSTRAP CSS TO THE ADMIN AREA
你的函数 main_enqueue_style
不仅将你的主主题样式表排入队列,而且它调用 Bootstrap 作为依赖项。请参阅:https://developer.wordpress.org/reference/functions/wp_enqueue_style/ 了解更多信息。
所以实际上,您正在排队您的主要主题样式表,这就是该函数的目的。您需要设置一个单独的函数来仅入队 Bootstrap。
但是,您可能需要查看 admin_enqueue_scripts 挂钩以了解为管理端添加样式表的正确方法。引用WordPress自己的文档,就是:
"proper hook to use when enqueuing scripts and styles that are meant to be used in the administration panel. Despite the name, it is used for enqueuing both scripts and styles."
在上面的文档中,您可以找到几个不同的示例,说明如何为您的管理员正确添加 bootstrap 样式表。例如,您可以创建一个单独的函数,例如:
function myunique_namespace_bootstrap_enqueue() {
wp_enqueue_style('bootstrap');
}
然后
add_action( 'admin_enqueue_scripts', 'myunique_namespace_bootstrap_enqueue' );
如果您只需要在管理员端添加自定义 stylesheet/your 自己的 styles/bootstrap css。尝试两个 wordpress 函数 wp_register_style() and wp_enqueue_style()
function wpCustomStyleSheet(){
//first register sthe style sheet and then enqueue
wp_register_style( 'adminCustomStyle', get_bloginfo('stylesheet_directory') . '/adminCustomStyle.css', false, '1.0.0' );
wp_enqueue_style( 'adminCustomStyle' );
}
add_action('admin_enqueue_scripts', 'wpCustomStyleSheet');
我正在创建一个新的自定义 Wordpress 主题,我正在尝试将 Boostrap CSS 添加到管理区域,但只有当我也将 style.css 添加到管理区域时它才有效. Boostrap js 正在添加到管理区域,但 CSS 没有,如何添加 Bootstrap CSS 而不将 style.css 添加到管理区域?
这是我的function.php
<?php
/*
* Function to register bootstrap css style
*/
function bootstrap_enqueue_styles() {
wp_register_style('bootstrap', get_template_directory_uri().'/bootstrap/css/bootstrap.min.css');
}
/*
* Function to register theme css style
*/
function main_enqueue_style(){
$dependencies = array('bootstrap');
wp_enqueue_style('main-css-style', get_stylesheet_uri(), $dependencies);
}
/*
* Function to register Bootstrap javascript
*/
function bootstrap_enqueue_scripts() {
$dependencies = array('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri().'/bootstrap/js/bootstrap.min.js', $dependencies, '3.3.6', true );
}
add_action( 'wp_enqueue_scripts', 'bootstrap_enqueue_styles' );
add_action( 'wp_enqueue_scripts', 'main_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'bootstrap_enqueue_scripts' );
add_action( 'admin_init', 'bootstrap_enqueue_styles' );
add_action( 'admin_init', 'bootstrap_enqueue_scripts' );
//add_action( 'admin_init', 'main_enqueue_style' ); <-- IF I DONT ADD THIS ONE IT DOESNT ADD BOOTSTRAP CSS TO THE ADMIN AREA
你的函数 main_enqueue_style
不仅将你的主主题样式表排入队列,而且它调用 Bootstrap 作为依赖项。请参阅:https://developer.wordpress.org/reference/functions/wp_enqueue_style/ 了解更多信息。
所以实际上,您正在排队您的主要主题样式表,这就是该函数的目的。您需要设置一个单独的函数来仅入队 Bootstrap。
但是,您可能需要查看 admin_enqueue_scripts 挂钩以了解为管理端添加样式表的正确方法。引用WordPress自己的文档,就是:
"proper hook to use when enqueuing scripts and styles that are meant to be used in the administration panel. Despite the name, it is used for enqueuing both scripts and styles."
在上面的文档中,您可以找到几个不同的示例,说明如何为您的管理员正确添加 bootstrap 样式表。例如,您可以创建一个单独的函数,例如:
function myunique_namespace_bootstrap_enqueue() {
wp_enqueue_style('bootstrap');
}
然后
add_action( 'admin_enqueue_scripts', 'myunique_namespace_bootstrap_enqueue' );
如果您只需要在管理员端添加自定义 stylesheet/your 自己的 styles/bootstrap css。尝试两个 wordpress 函数 wp_register_style() and wp_enqueue_style()
function wpCustomStyleSheet(){
//first register sthe style sheet and then enqueue
wp_register_style( 'adminCustomStyle', get_bloginfo('stylesheet_directory') . '/adminCustomStyle.css', false, '1.0.0' );
wp_enqueue_style( 'adminCustomStyle' );
}
add_action('admin_enqueue_scripts', 'wpCustomStyleSheet');