为什么我的wordpress插件没有显示侧边栏



我正在php中为wordpress编程一个插件,用于管理小部件、它们在页面中的位置以及它们应该出现在哪个页面中。我有问题,因为我无法激活我的侧边栏,我不知道为什么。我发布我的代码是为了看看是否有什么不对劲,你可以帮助我。第一个文件是functions.php,理论上应该注册边栏。

require_once('widgets.php');
function my_register_sidebars(){
// Register the 'primary' sidebar. 
register_sidebar(
array(
'id' => 'primary',
'name' => __('Primary Sidebar'),
'description' => __('A short description of the sidebar.'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'id' => 'adios',
'name' => __('adios'),
'description' => __('A short description of the sidebar.'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
}
add_action('widgets_init', 'my_register_sidebars');

经过一系列的尝试,我也看到add_action没有完成他的工作,你对此有答案吗?之后,我实例化了一个新文件(sidebar.php(来调用侧边栏。

<?php
if (is_active_sidebar('primary')) : ?>
<aside id="secondary" class="sidebar widget-area" role="complementary" style="background-color = 'black';">
<?php dynamic_sidebar('primary'); ?>
</aside><!-- .sidebar .widget-area -->
<?php endif; ?>

最后,为了调用侧边栏,我使用get_sidebar((;但我无法显示侧边栏,它似乎甚至都不活跃。你对我如何在这个"激活"的侧边栏中添加小部件有什么想法吗?谢谢你的帮助。

在函数中使用此代码。php

function my_custom_sidebar() {
register_sidebar(
array (
'name' => __( 'Custom', 'your-theme-domain' ),
'id' => 'custom-side-bar',
'description' => __( 'Custom Sidebar', 'your-theme-domain' ),
'before_widget' => '<div class="widget-content">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);} add_action( 'widgets_init', 'my_custom_sidebar' );

并将此代码放入您的模板文件

<?php if ( is_active_sidebar( 'custom-side-bar' ) ) : ?>
<?php dynamic_sidebar( 'custom-side-bar' ); ?>

试试这个代码

最新更新