我正在学习短代码。我已经做了2个短码,可以用引导程序制作手风琴。一个短代码(accordionGroup)创建包含一个或多个短代码的主体,以生成项目及其内容(accordionItem)。
现在我需要创建两个不同的手风琴。问题是:我需要为每个手风琴使用不同的id,并将其解析为手风琴中的每个项目。那就是制作手风琴动画。
这是代码
//This is the accordion container
function accordionGroup( $atts, $content = null )
{
extract(shortcode_atts(array(
\'id_container\' => \'\',
), $atts));
return "<div class=\'panel-group\' id=".$id_container." role=\'tablist\' aria-multiselectable=\'true\'>".do_shortcode($content)."</div>";
}
add_shortcode( \'accordion-group\', \'accordionGroup\');
//This is the accordion item. It\'s need a reference from the accordion ID
function accordionItem( $atts, $content = null)
{
extract(shortcode_atts(array(
\'titulo\' => \'\',
\'id\' => \'\',
), $atts));
return "
<div class=\'panel panel-default\'>
<div class=\'panel-heading\' role=\'tab\' id=".$id.">
<h4 class=\'panel-title\'>
<a class=\'collapsed\' role=\'button\' data-toggle=\'collapse\' data-parent=\'#the-reference-goes-here\' href=\'#".$id."-collapse\' aria-expanded=\'false\' aria-controls=".$id."-collapse\'>".$titulo."</a>
</h4>
</div>
<div id=\'".$id."-collapse\' class=\'panel-collapse collapse\' role=\'tabpanel\' aria-labelledby=".$id.">
<div class=\'panel-body\'>".$content."</div>
</div>
</div>";
}
add_shortcode( \'accordion-item\', \'accordionItem\');
SO网友:s_ha_dum
您可以通过global
但将其困在静态变量中会更整洁:
function track_acc_id($new_id = false) {
static $id;
if (!empty($new_id)) {
$id = $new_id;
}
return $id;
}
//This is the accordion container
function accordionGroup( $atts, $content = null )
{
extract(shortcode_atts(array(
\'id_container\' => \'\',
), $atts));
track_acc_id($id_container);
return "<div class=\'panel-group\' id=".$id_container." role=\'tablist\' aria-multiselectable=\'true\'>".do_shortcode($content)."</div>";
}
add_shortcode( \'accordion-group\', \'accordionGroup\');
//This is the accordion item. It\'s need a reference from the accordion ID
function accordionItem( $atts, $content = null)
{
extract(shortcode_atts(array(
\'titulo\' => \'\',
\'id\' => \'\',
), $atts));
$id = track_acc_id();
return "
<div class=\'panel panel-default\'>
<div class=\'panel-heading\' role=\'tab\' id=".$id.">
<h4 class=\'panel-title\'>
<a class=\'collapsed\' role=\'button\' data-toggle=\'collapse\' data-parent=\'#the-reference-goes-here\' href=\'#".$id."-collapse\' aria-expanded=\'false\' aria-controls=".$id."-collapse\'>".$titulo."</a>
</h4>
</div>
<div id=\'".$id."-collapse\' class=\'panel-collapse collapse\' role=\'tabpanel\' aria-labelledby=".$id.">
<div class=\'panel-body\'>".$content."</div>
</div>
</div>";
}
add_shortcode( \'accordion-item\', \'accordionItem\');
echo do_shortcode(\'[accordion-group id_container="d00d"]\');
echo do_shortcode(\'[accordion-item]\');
或者把整件事都放到一个班里。