dynamic_sidebar()函数完全不起作用。

huangapple go评论71阅读模式
英文:

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(
        &#39;id&#39; =&gt; &#39;header-2&#39;,
        &#39;name&#39; =&gt; &#39;the second hader&#39;

    ));
}
add_action(&#39;widgets_init&#39;, &#39;my_theme_add_widget_support&#39;);

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:

&lt;?php
if (is_active_sidebar(&#39;header-2&#39;)) {
    echo &#39;active&#39;;
    dynamic_sidebar(&#39;header-2&#39;)
} else {
    echo &#39;not active&#39;;
}
?&gt;

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:

&lt;?php
if (is_active_sidebar(&#39;header-2&#39;)) {
    echo &#39;active&#39;;
    dynamic_sidebar(&#39;header-2&#39;);
} else {
    echo &#39;not active&#39;;
}
?&gt;

huangapple
  • 本文由 发表于 2023年2月14日 21:33:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定