英文:
The function dynamic_sidebar() is not working at all
问题
我正在开发一个WordPress主题,需要在我的页眉部分添加小部件,所以我在functions.php中使用了register_sidebar()
:
function my_theme_add_widget_support()
{
register_sidebar(array(
'id' => 'header-2',
'name' => 'the second header'
));
}
add_action('widgets_init', 'my_theme_add_widget_support');
然后在header.php文件中,我在想要显示小部件的位置写了以下代码,但它没有显示任何内容。我进行了一个测试,看它是否激活了,结果发现它根本没有激活;尽管我确保在我的管理面板中的小部件区域中,小部件内有一些内容:
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2');
} else {
echo 'not active';
}
?>
这个测试始终返回“not active”。
附注:我正在使用小部件编辑器的旧版本。
英文:
I'm developing a WordPress theme and I needed to add widgets to my header section so I used register_sidebar()
in my functions.php:
function my_theme_add_widget_support()
{
register_sidebar(array(
'id' => 'header-2',
'name' => 'the second hader'
));
}
add_action('widgets_init', 'my_theme_add_widget_support');
And then in the file header.php I wrote this code in the place where I wanted the widget to display but it doesn't display anything. I did a test to see if it's active and I found out that it's not active at all; even though I made sure that in the widgets area in my admin panel, the widget has something inside it:
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2')
} else {
echo 'not active';
}
?>
This test always return not active.
PS: I'm using the old version of widgets editor.
答案1
得分: 0
根据您的代码,可能存在语法问题或不正确的参数配置。尝试使用正确的参数更新您的 register_sidebar()
函数,并确保在 dynamic_sidebar('header-2')
后面添加分号。以下是更新后的代码:
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2');
} else {
echo 'not active';
}
?>
英文:
As per your code, there might be a syntax issue or incorrect parameter configuration. Try updating your register_sidebar() function with the correct parameters and make sure to add a semicolon after dynamic_sidebar('header-2'). Here's the updated code:
<?php
if (is_active_sidebar('header-2')) {
echo 'active';
dynamic_sidebar('header-2');
} else {
echo 'not active';
}
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论