我正在编写我的第二个WordPress插件,我成功地将JavaScript和CSS(Bootstrap)排入后端设置页面。现在,要与前端进行交互,我想使用Bootstrap创建一些短代码。如何仅在我的短代码中使用Bootstrap(以及其他自定义CSS和JavaScript)而不影响将使用短代码的页面的样式?
您可以使用唯一的类名在内容周围放置div容器。
function shortcode_func(){
return '<div class="your-unique-shortcode-class">...</div>';
}
add_shortcode( 'your-shortcode', 'shortcode_func' );
您可以使用wp_enqueue_style
函数添加样式表。
function shortcode_footer_func() {
wp_enqueue_style( 'shortcode_styles', 'your/path/to/style.css');
}
add_action( 'wp_footer', 'shortcode_footer_func' );