添加后左侧边栏未显示

时间:2018-03-23 19:52:56

标签: php wordpress twitter-bootstrap-3

我一直在尝试根据GreatMag主题制作的儿童主题添加一个左侧栏,默认情况下有一个侧边栏(右侧边栏)。我无法显示左侧边栏,我已经查了很多关于如何执行此操作的教程,例如this one,我明白需要做什么:

  1. 创建子主题
  2. functions.php 文件中注册新的侧边栏

  3. 在我的案例中添加新模板文件 sidebar-left.php

  4. 设置新侧边栏的样式 - 我将它放在css
  5. 在您希望其显示的页面上的文件中调用侧栏: get_sidebar('sidebar-2');
  6. 我注册了新的左侧边栏:

    register_sidebar( array(
            'name'          => esc_html__( 'Left Sidebar', 'greatmag' ),
            'id'            => 'sidebar-2',
            'description'   => esc_html__( 'Add widgets here.', 'greatmag' ),
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget'  => '</section>',
            'before_title'  => '<h2 class="widget-title">',
            'after_title'   => '</h2>',
        ) );
    

    这就是sidebar-left.php的样子:

    <?php
    /**
     * The sidebar containing the main widget area.
     *
     * @link https://developer.wordpress.org/themes/basics/template-files/#template-partials
     *
     * @package GreatMag
     */
    
    if ( ! is_active_sidebar( 'sidebar-2' ) ) {
        return;
    }
    ?>
    
    <aside id="left" class="widget-area col-md-2 sidebar-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-2' ); ?>
    </aside><!-- #secondary -->
    

    我将两个侧边栏的指定类更改为:col-md-2,因为主要内容为col-md-8

    左侧边栏显示在仪表板中,因此我重新对其进行了注册: enter image description here

    但是只有右侧边栏显示在窗口小部件区域中: enter image description here

    也没有在主页上显示: enter image description here

    我还看了few themes that have left and right sidebars看看它是如何完成但最终没有成功的,所以如果有人能向我解释我做错了什么,我会非常感激!

2 个答案:

答案 0 :(得分:2)

您的问题如下:

  • 您是registering侧边栏,它可以在小部件区域中使用。
  • 您正在sidebar-left.php文件
  • 中定义补充工具栏布局
  • 但你 实际上是在告诉你的主题在任何地方拉边栏。

简要地看一下主题,他们在一些主题文件中用get_sidebar()调用侧边栏。所以你有两个主要选择。

<强>一: 将这些文件复制到您的子主题并在同一文件中添加get_sidebar('sidebar-left');,展示位置可能会有所不同,但这些是以这种方式调用侧边栏的文件:

使用此方法,您还可以创建新的页面模板,例如two-sidebars.php,并将get_sidebar('sidebar-left');代码放在该文件中,这样您就可以选择在每页的基础上使用两个侧边栏(或如果您愿意,甚至只需要左侧边栏,删除get_sidebar();行)

Screenshot of Theme Structure

<强>两个 使用add_action()在侧栏中挂钩。

我可能会坚持使用第一个选项,因为它是调用主题默认侧边栏的方式。

答案 1 :(得分:1)

    function wpb_widgets_init() {

        register_sidebar( array(
            'name' =>__( 'Sidebar Title', 'wpb'),
            'id' => 'sidebar-new',
            'description' => __( 'Description goes here', 'wpb' ),
            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
            'after_widget' => '</aside>',
            'before_title' => '<h3 class="widget-title">',
            'after_title' => '</h3>',
        ) );
        }

    add_action( 'widgets_init', 'wpb_widgets_init' );


    //to display 

    <?php if ( is_active_sidebar( 'sidebar-new' ) ) : ?>
        <div id="secondary" class="widget-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-new' ); ?>
        </div>
    <?php endif; ?>

请参阅以下链接以显示侧栏 https://developer.wordpress.org/reference/functions/register_sidebar/ http://www.wpbeginner.com/wp-themes/how-to-add-dynamic-widget-ready-sidebars-in-wordpress/

相关问题