我已经创建了一个自定义WordPress主题,需要能够将侧栏包含在小部件中。我有侧边栏。php文件、函数代码和三个小部件等着见分晓。但是,唉,没有骰子。
这是侧边栏。php代码:
<?php
/**
* The sidebar containing the main widget area
*
* If no active widgets are in the sidebar, hide it completely.
*
* @package WordPress
* @subpackage Twenty_Twelve
* @since Twenty Twelve 1.0
*/
?>
<?php if ( is_active_sidebar( \'sidebar-1\' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( \'sidebar-1\' ); ?>
</div><!-- #secondary -->
<?php endif; ?>
下面是函数调用:
// Declare sidebar widget zone
if (function_exists(\'register_sidebar\')) {
register_sidebar(array(
\'name\' => \'Sidebar Widgets\',
\'id\' => \'sidebar-widgets\',
\'description\' => \'These are widgets for the sidebar.\',
\'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2>\',
\'after_title\' => \'</h2>\'
));
}
if (function_exists(\'register_sidebar\')) {
register_nav_menus(
array(
\'main_nav\' => \'Main Navigation Menu\'
)
);
}
我调用侧栏如下:
<?php get_sidebar(); ?>
我将非常感谢您的帮助,以正确显示此内容。
最合适的回答,由SO网友:Aibrean 整理而成
您的问题是所调用的动态侧栏与注册内容的ID不匹配。
匹配:
<?php if ( is_active_sidebar( \'sidebar-widgets\' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( \'sidebar-widgets\' ); ?>
</div><!-- #secondary -->
<?php endif; ?>
寄存器侧栏
if (function_exists(\'register_sidebar\')) {
register_sidebar(array(
\'name\' => \'Sidebar Widgets\',
\'id\' => \'sidebar-widgets\',
\'description\' => \'These are widgets for the sidebar.\',
\'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',
\'after_widget\' => \'</div>\',
\'before_title\' => \'<h2>\',
\'after_title\' => \'</h2>\'
));
}
<?php get_sidebar(); ?>
告诉模板使用
sidebar.php
文件