如何将寄存器侧边栏的‘BEFORE_WIDGET’唯一id生成器用于它的其他参数,如‘BEFORE_TITLE’?

时间:2021-04-16 作者:Janfred Magnaye

我正在尝试创建一个动态侧栏,它使用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>
有什么方法可以做到这一点吗?谢谢

1 个回复
最合适的回答,由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;
});

相关推荐

如何创建4列和2列Twitter-Bootstrap相结合的WordPress循环?

我想根据4列(桌面视图)、2列(mobile view 768px)和1列(mobile view 425px)的组合显示帖子。我在下面找到了很棒的代码:<?php $paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1; $args=array( \'post_type\' => \'post\',