如何仅将CSS应用于我的自定义WordPress短代码?

问题描述 投票:0回答:1

我正在编写我的第二个WordPress插件,我成功地将JavaScript和CSS(Bootstrap)排入后端设置页面。现在,要与前端进行交互,我想使用Bootstrap创建一些短代码。如何仅在我的短代码中使用Bootstrap(以及其他自定义CSS和JavaScript)而不影响将使用短代码的页面的样式?

javascript css wordpress plugins shortcode
1个回答
1
投票

您可以使用唯一的类名在内容周围放置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' );
© www.soinside.com 2019 - 2024. All rights reserved.