我想将CSS添加到不依赖于主题、插件或任何东西的页面或短代码(最好是短代码)。
我尝试添加wp_enqueue_style(\'[my-css-url]\');
到各种PHP文件,但没有成功。然而,我希望有更合适的方法来做到这一点。
我有办法吗?
这个问题不是重复的Enqueue Scripts / Styles when shortcode is present另一个问题是在插件的上下文中提出和解决的。我的问题明确指出“独立于插件”。
我想将CSS添加到不依赖于主题、插件或任何东西的页面或短代码(最好是短代码)。
我尝试添加wp_enqueue_style(\'[my-css-url]\');
到各种PHP文件,但没有成功。然而,我希望有更合适的方法来做到这一点。
我有办法吗?
这个问题不是重复的Enqueue Scripts / Styles when shortcode is present另一个问题是在插件的上下文中提出和解决的。我的问题明确指出“独立于插件”。
只需点击链接has_shortcode()
中的文档last answer 重复的问题。在这里,您将发现:
function custom_shortcode_scripts() {
global $post;
if ( is_a( $post, \'WP_Post\' ) && has_shortcode( $post->post_content, \'custom-shortcode\') ) {
wp_enqueue_script( \'custom-script\');
}
}
add_action( \'wp_enqueue_scripts\', \'custom_shortcode_scripts\');
对于样式,这将是:function custom_shortcode_styles() {
global $post;
if ( is_a( $post, \'WP_Post\' ) && has_shortcode( $post->post_content, \'custom-shortcode\') ) {
wp_enqueue_style(\'custom-shortcode-css\', get_template_directory_uri() . \'/css/custom-shortcode.css\');
}
}
add_action( \'wp_enqueue_scripts\', \'custom_shortcode_styles\');
更换custom-shortcode
在…内has_shortcode()
使用实际要查找的短代码,然后将该代码片段放入functions.php
或者在你的插件和代码上!我想使用引导css向我的wordpress页面添加一个响应div,就在我将引导样式排入队列时,它会释放主题,所以我想使用ajax调用加载该div,在文档准备就绪后调用该排入队列的样式,然后我将使用jquery addClass()添加css类。这样,我的自定义css就不会影响主题样式