在我的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
的内容。
也:
- 将
sidebar.php
重命名为sidebar-lj_sidebar.php
,或 - 将您的侧边栏呼叫从
get_sidebar( 'lj_sidebar' );
更改为get_sidebar();
。