我目前正在为wordpress开发一个插件。我的插件内容是在定义的短代码上触发的。我将一个参数传递给短代码,以生成一些条件。我能够有条件地加载JS,但我也需要有条件地加载CSS。
假设我使用了短代码:myshortcode css=“深色”,我做了一个数据库查询,并注入了想要的css。
这有可能吗?我读过一些关于这件事的文章。我知道这是因为密码是在头部被装上子弹后发射的。我没能找到解决办法。
我有什么选择?
谢谢!
发布于 2016-11-17 16:57:18
发布于 2016-11-17 17:39:04
还有更好的办法。您可以简单地通过register或wp_register_script将您的css和脚本与wp_enqueue_scripts联系起来,然后从您的短代码中将已注册的脚本排成队列。您将有机会根据特定条件对脚本进行排队。
下面是一个代码示例。
/**
* Register scripts
*/
function my_plugin_scripts() {
wp_register_style( 'dark-css', $css_path );
wp_register_script( 'script-name', $js_path, array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );
/**
* My Shortcode
*/
function my_plugin_shortcode( $atts ) {
$atts = shortcode_atts( array(
'css' => 'default'
), $atts );
if ( $atts['css'] == 'dark') {
wp_enqueue_style('dark-css')
}
// do shortcode actions here
}
add_shortcode( 'shortcode-id','my_plugin_shortcode' );发布于 2016-11-17 17:42:17
如果我正确理解了您的问题,您可以这样做来加载一个样式表或另一个样式表:
function aw_shortcode_function($atts) {
$a = shortcode_atts( array(
'css' => 'light', // this is the default value
), $atts );
$colorTheme = $a['css'];
if ($colorTheme == 'dark') {
// load/enqueue/get/do whatever based on the css="dark" shortcode attribute
}
}https://stackoverflow.com/questions/40660101
复制相似问题