我试图使用一组页面模板(如日期、存档、标记等)创建8个侧边栏区域,然后在数组中循环,为每个值创建一个侧边栏。
我认为应该这样做,但它只会为数组中的最后一个值创建一个侧栏:
$widget_areas = array(\'single\',\'index\',\'category\',\'archive\',\'tag\',\'taxonomy\',\'author\',\'date\',\'search\',\'attachment\');
foreach($widget_areas as $area){
register_sidebar(
array(
\'name\' => __( $area. \' Widgets\' ),
\'id\' => \'index\',
\'description\' => __( \'Widgets in this area will appear on the \' . $area ),
\'before_widget\' =>\'<div class="widget-container">\',
\'before_title\' => \'<h3>\',
\'after_title\' => \'</h3>\',
\'after_widget\' => \'</div>\'
)
);
}
它创建的唯一侧栏是“附件”模板。有什么好处?不应该为该数组中的每个值运行register\\u提要栏,从而创建大量提要栏?如果我用register\\u侧栏对每个部分进行硬编码,但我真的不想重复一堆代码
最合适的回答,由SO网友:fuxia 整理而成
传递不同的id
对于每个侧栏,或者您将一次又一次地覆盖相同的侧栏。您也可以离开id
参数,WordPress将自动创建一个参数。
旁注:
__( $area. \' Widgets\' )
以及
__( \'Widgets in this area will appear on the \' . $area )
…是错误的。转换函数需要第二个参数
textdomain
, 并且不能在应该翻译的字符串中使用变量。
使用sprintf()
使用占位符传递变量,并使用上下文函数解释参数:
sprintf(
_x(
\'%s Widgets\',
\'%s = widget area name\',
\'your_theme_textdomain\'
),
$area
)