<?php
// my shortcode like
/*
[accordion]
[section title="Your title #1" class="active" ]
Your Description here #1
[/section]
[section title="Your title #2"]
Your Description here #2
[/section]
[section title="Your title #3"]
Your Description here #3
[/section]
[/accordion]
*/
add_shortcode('section', 'section');
function section($atts, $content){
extract(shortcode_atts(array(
'title' => 'Collapse',
'id' => false,
'class' => false,
), $atts));
$GLOBALS['section'][] = array(
'title' => $title ,
'id' => $id,
'class' => $class ,
'content' => $content ,
);
$id = "collapse-id-".$GLOBALS['collapsibles_count'];
foreach($GLOBALS['section'] as $tab){
$class = (!empty($tab['class']) && $tab['class']=="active") ? "panel-collapse collapse in" : "panel-collapse collapse";
$__title = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($tab['title']) );
$return = sprintf("\n".'<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#%s" href="#%s">%s</a></h4></div><div id="%s" class="%s"><div class="panel-body">%s</div></div></div>'."\n",$id, $__title, $tab['title'], $__title, $class, $tab['content']);
} // foreach
return do_shortcode($return);
}// function ending
add_shortcode('accordion', 'accordion');
function accordion($atts, $content){
if(isset($GLOBALS['collapsibles_count'])) {
$GLOBALS['collapsibles_count']++;
}else {
$GLOBALS['collapsibles_count'] = 0;
}
$id = "collapse-id-".$GLOBALS['collapsibles_count'];
return do_shortcode(sprintf('<div class="tp-accordion"><div class="panel-group" id="%s"> %s </div></div> ', $id, $content));
}
?>
wordpressshortcodebootstrap
提供的代碼可能有幫助,但爲了使其成爲一個很好的答案,你也應該描述/解釋/究竟怎樣你的代碼解決了這個問題。 – ZygD