我知道这听起来很复杂,所以请耐心听我说。
我正在尝试根据当前帖子/页面中的短代码(如果有)添加某些自定义css样式。
function load_shortcode_styles($posts){
//function to check short codes here
//regex to get id from shortcode(e.g [item id=""] )
//get custom post meta from the id that has my custom css
add_action(\'wp_head\', create_function(\'\', \'echo "\\n<!-- my style -->\\n<style>\' . $custom_css . \'</style>\\n";\') );
}
add_filter(\'the_posts\', \'load_shortcode_styles\' );
打开帖子页面后,源代码显示我的风格被打印了两次,如下所示:<!-- my style -->
<style>
</style>
<!-- my style -->
<style>
</style>
出于测试目的,我使用了一个硬编码函数来回显某个字符串,它工作得很好。e、 g.:
function test_print(){
echo "\\n<!-- my style -->\\n<style>foo bar</style>\\n";
}
并用这个替换上面的行动钩add_action(\'wp_head\', \'test_print\' );
不知何故,这将打印出来,这是正确的!<!-- my style -->
<style>
foo bar
</style>
有人知道这个问题吗?我如何解决这个问题?