我已使用以下代码注册了一个侧栏:
function reg_l_sid(){
$args = array(
\'name\' => __( \'left-sidebar\', \'Tutorial-Blog\' ),
\'id\' => \'left-sidebar\',
\'description\' => \'\',
\'class\' => \'\',
\'before_widget\' => \'<li id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</li>\',
\'before_title\' => \'<h2 class="widgettitle">\',
\'after_title\' => \'</h2>\' );
register_sidebar( $args );
}
add_action (\'widgets_init\',\'reg_l_sid\');
侧栏已经注册,然后我添加了一些小部件。然后我尝试使用条件
is_active_sidebar
要在没有类似的小部件时显示消息,请执行以下操作:
if ( is_active_sidebar( \'left-sidebar\' ) ) {
dynamic_sidebar( \'left-sidebar\' );
} else {
echo \'Please add widgets\';
}
但它一直显示消息“请添加小部件”,正如我提到的,已经添加了一些小部件。那么问题出在哪里呢?
使现代化
需要指出的是,它在没有条件的情况下运行良好。
最合适的回答,由SO网友:Sonali 整理而成
请试试这个,因为如果没有id,我认为这是行不通的:
function reg_l_sid()
{
$args = array( \'id\' => \'sidebar-footer-6\',\'name\'=> \'Left-sidebar\');
register_sidebar($args);
}
add_action(\'widgets_init\', \'reg_l_sid\');
和内部模板:
if (is_active_sidebar(\'sidebar-footer-6\')) {
dynamic_sidebar(\'sidebar-footer-6\');
} else {
echo \'Please add widgets\';
}
SO网友:Mohamed Omar
事实上,问题已经解决了,但我不知道为什么会这样。。因此,在使用默认WordPress函数后,一切都很好,然后我设法删除了不需要的参数,返回到只使用name参数。。。这是我的出发点。。在检查了每件事后。。所以我的be是一个语法问题,但使用name
仅限参数。现在我有了这个,它工作得很好:
function reg_l_sid(){
$args = array(\'name\'=> __( \'Left-sidebar\', \'Tutorial-Blog\' ));
register_sidebar( $args );
}
add_action (\'widgets_init\',\'reg_l_sid\')
在侧栏中:
if ( is_active_sidebar( \'Left-sidebar\' ) ) {
dynamic_sidebar( \'Left-sidebar\' );
} else {
echo \'Please add widgets\';
}
SO网友:lalitpendhare
我刚刚将您的代码放在我的函数文件中,并创建了一个模板来测试您的代码。对我来说,它工作得很好。请重新评估您的物品,它可能会起作用。
add_action( \'widgets_init\', \'twentyseventeen_widgets_init\' );
function reg_l_sid(){
$args = array(
\'name\' => __( \'left-sidebar\', \'Tutorial-Blog\' ),
\'id\' => \'left-sidebar\',
\'description\' => \'\',
\'class\' => \'\',
\'before_widget\' => \'<li id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</li>\',
\'before_title\' => \'<h2 class="widgettitle">\',
\'after_title\' => \'</h2>\' );
register_sidebar( $args );
}
add_action (\'widgets_init\',\'reg_l_sid\');
if ( is_active_sidebar( \'left-sidebar\' ) ) {
dynamic_sidebar( \'left-sidebar\' );
} else {
echo \'Please add widgets\';
}