WordPress侧边栏不可见


Wordpress sidebar not visible

在我的footer.php中添加了<?php get_sidebar('lj_sidebar'); ?>在我的functions.php中,我启用了小部件:add_theme_support('widgets');并制作了一个动作钩子add_action('widgets_init', 'lj_widgets');

然后在我widgets.php中,我使小部件如下所示:

<?php
function lj_widgets() {
    register_sidebar(array(
        'name'          => __('My First Theme Footer','lnj'),
        'id'            => 'lj_sidebar',
        'description'   => __('Footer for my first theme','lnj'),
        'class'         => '',
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget'  => '</li>',
        'before_title'  => '<h2>',
        'after_title'   => '</h2>'
    ));
}

在我的sidebar.php中,我检查侧边栏是否已启用:

<?php
if(is_active_sidebar('lj_sidebar')) {
    dynamic_sidebar('lj_sidebar');
}

该小部件在我的管理面板中可见。但是当我在其中放置一个文本字段时。它没有出现?

我在这里做错了什么?

当你调用get_sidebar( 'lj_sidebar' );时,WordPress正在查找文件sidebar-lj_sidebar.php而不是sidebar.php

您正确地调用了侧边栏,dynamic_sidebar( 'lj_sidebar' );上面写着"显示带有 id lj_sidebar的小部件区域",但它没有显示在前端,因为您告诉 WordPress 显示可能不存在的sidebar-lj_sidebar.php的内容。

也:

  1. sidebar.php重命名为 sidebar-lj_sidebar.php ,或
  2. 将您的侧边栏呼叫从 get_sidebar( 'lj_sidebar' ); 更改为 get_sidebar();