这个问题似乎和asked here 但答案却走了另一条路。
如果用户输入一个带有空格的快捷码属性,它将被默认值覆盖,我不知道如何停止这种行为(WP shortcode API文档中没有提到)
Shorcode(短代码):
[imageaccordion title="We\'re awesome"]Content Here[/imageaccordion]
PHP:
function image_accordion( $atts , $content = null ) {
// Attributes
extract(
shortcode_atts(
array(
\'title\' => \'Accordion Title\',
), $atts , \'imageaccordion\'
)
);
// Put It Together
ob_start();
?>
<div class=\'image-accordion\'>
<h2>
<?php echo $title; ?>
</h2>
<?php
$outputbefore = ob_get_clean();
$outputafter = \'</div>\';
//Return
return $outputbefore . do_shortcode($content) . $outputafter;
}
add_shortcode( \'imageaccordion\', \'image_accordion\' );
我似乎得到了
<div class="image-accordion\'>
<h2>
Accordion Title
</h2>
</div>
每次。。。
如果我在$ATT上运行print\\r,我会得到
Array ( [0] =>
/> [2] => title="We\'re [3] => Awesome")
这是在新安装的WP上,没有运行默认主题的插件
(也减少了eg.代码)
SO网友:Abhik
问题似乎出在输出缓冲中。为什么不使用赋值运算符呢?
function image_accordion( $atts , $content = null ) {
// Attributes
$args = shortcode_atts( array(
\'title\' => \'Accordion Title\',
), $atts , \'imageaccordion\' );
$output = \'<div class="image-accordion">\';
$output .= \'<h2>\' . $args[\'title\'] . \'</h2>\';
$output .= \'</div>\';
$output .= do_shortcode($content);
return $output;
}
add_shortcode( \'imageaccordion\', \'image_accordion\' );