我正在尝试创建一个动态侧栏,它使用bootstrap 4 accordion,小部件标题作为切换链接,小部件内容作为accordion的内容:
计划布局:
<div class="card">
<div class="card-header">
<a class="collapsed card-link" data-toggle="collapse" href="#faq-6">
Widget Title
</a>
</div>
<div id="faq-6" class="collapse" data-parent="#accordion">
<div class="card-body">
<p>Widget Content</p>
</div>
</div>
</div>
寄存器边栏代码:
register_sidebar( array(
\'name\' => __( \'FAQ\', \'rocket\' ),
\'id\' => \'faq-tabs\',
\'description\' => __( \'Displays FAQ Tabs\', \'rocket\' ),
\'before_sidebar\' => \'<div id="accordion">\',
\'after_sidebar\' => \'</div>\',
\'before_widget\' => \'<div class="card"><div class="card-header"><a class="collapsed card-link" data-toggle="collapse" href="#%1$s">\',
\'after_widget\' => \'</div></div></div>\',
\'before_title\' => \'<h3 class="card-title">\',
\'after_title\' => \'</h3></a><div id="%1$s" class="collapse" data-parent="#accordion">\',
) );
但在前端渲染时
\'after_title\'
参数使用/呈现的ID与
\'before_widget\'
用途:
<div id="accordion">
<div class="widget_text card">
<div class="widget_text card-header">
<a class="widget_text collapsed card-link" data-toggle="collapse" href="#custom_html-14"><h3 class="card-title">Widget Title</h3></a>
<div id="%1$s" class="collapse" data-parent="#accordion">
<div class="textwidget custom-html-widget"><p>Widget Content</p></div>
</div>
</div>
</div>
有什么方法可以做到这一点吗?谢谢
最合适的回答,由SO网友:Andrew J Klimek 整理而成
您可以使用dynamic_sidebar_params 筛选以进行您自己的替换。
类似这样:
add_filter(\'dynamic_sidebar_params\', function($params){
$params[0][\'after_title\'] = str_replace( \'%1$s\', $params[0][\'widget_id\'], $params[0][\'after_title\'] );
return $params;
});