【问题标题】:Wordpress dynamic_sidebar not parsing, deleting changesWordpress dynamic_sidebar 不解析,删除更改
【发布时间】:2016-09-28 10:44:34
【问题描述】:

各位程序员,

我对 Wordpress 比较陌生,因为我通常使用 Typo3,所以我对 WP 中的自定义侧边栏有疑问。

我的functions.php中有以下代码

function custom_sidebars()
{

  register_sidebars(4, array
  (
    'name' => __('Panel Startseite %d', 'me'),
    'id' => 'panel-home-%d',
    'description' => 'Widget Position for home panel',
    'class' => 'panel-sidebar',
    'before_widget' => '<div class="widget-panel-%d">',
    'after_widget' => '</div>',
    'before_title' => '<h2 class="panel-title">',
    'after_title' => '</h2>'
  ));

  $args2 = array
  (
    'name' => __('Short Bio Sidebar', 'me'),
    'id' => 'shortbio-id',
    'description' => 'Widget Position for short bio',
    'class' => 'shortbio-sidebar',
    'before_widget' => '<div class="widget-bio">',
    'after_widget' => '</div>',
    'before_title' => '<h2 class="widgettitle">',
    'after_title' => '</h2>'
  );
  register_sidebars(1, $args2);

}

add_action('widgets_init', 'custom_sidebars');

我想在我的主页上添加 4 个信息面板,并在页脚中添加另一个小字段供作者添加或更改简历。

这是我的 index.php 中的代码

<div id="main">
  <div class="wrapper">
    <div class="col-sm-6">
      <div class="inner">
        <?php dynamic_sidebar('panel-home-1'); ?>
      </div>
    </div>
    <div class="col-sm-6">
      <div class="inner">
        <?php dynamic_sidebar('panel-home-2'); ?>
      </div>
 ... and so on

同样的实现在footer.php中的给定位置。

我的问题是:

  • 我通过后端菜单主题添加的内容 -> 小部件不显示,并且在我下次打开后端时被删除。
  • wp-content/debug.log 中没有错误,我在激活 wp-config.php 中的选项后修复了所有常见的错误

我该如何解决这个问题,我的基本思维错误是什么?

最好的问候斯凯洛

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    您的代码都很好。只是一个错误,您使用%d 连接您的id 后面的小数。但这不是必需的,因为 %d 会自动添加到提供的 id 中。请在此答案末尾找到 codex 链接。

    所以,我从代码的 id 部分中删除了 %d,现在它可以工作了。

    解决方案:

    function custom_sidebars()
    {
    
      register_sidebars(4, array
      (
        'name' => __('Panel Startseite %d', 'me'),
        'id' => 'panel-home',
        'description' => 'Widget Position for home panel',
        'class' => 'panel-sidebar',
        'before_widget' => '<div class="widget-panel">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="panel-title">',
        'after_title' => '</h2>'
      ));
    
      $args2 = array
      (
        'name' => __('Short Bio Sidebar', 'me'),
        'id' => 'shortbio-id',
        'description' => 'Widget Position for short bio',
        'class' => 'shortbio-sidebar',
        'before_widget' => '<div class="widget-bio">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="widgettitle">',
        'after_title' => '</h2>'
      );
      register_sidebars(1, $args2);
    
    }
    
    add_action('widgets_init', 'custom_sidebars');
    

    <div id="main">
      <div class="wrapper">
        <div class="col-sm-6">
          <div class="inner">
            <?php dynamic_sidebar('panel-home'); ?>
          </div>
        </div>
        <div class="col-sm-6">
          <div class="inner">
            <?php dynamic_sidebar('panel-home-2'); ?>
          </div>
     ... and so on
    

    您不需要将 %d 添加到 register_sidebars 的 id 中。请阅读codex, 它说:

    "%d" 在第一个之后自动添加到提供的 'id' 值;例如,“Sidebar-1”、“Sidebar-2”、“Sidebar-3”等

    【讨论】:

    • 感谢很多朋友!它现在按预期工作。但是等等,小部件管理界面上有大量不活动的侧边栏。我如何删除它们,也许隐藏它们?
    • 不客气。点击小部件,它会在底部显示删除选项。
    • 如果答案解决了您的问题,请不要忘记将其作为正确答案进行检查。接受答案有助于其他人跟踪您的问题是否已解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多