在 DWQA 中包含自定义侧边栏并删除主侧边栏

Include custom sidebar and remove main sidebar in DWQA

我想从 dwqa-question 页面中删除主侧边栏,并想在该页面中添加自定义侧边栏。

我在functions.php中写了一个代码:

    function dwqa_theme_register_sidebar() {
    register_sidebar( array(
    'name'          => __( 'Single Question', 'multinews' ),
    'id'            => 'dqwa',
    'class'         => '',
    'before_widget' => '<section id="%1$s" class="widget %2$s">',
    'after_widget'  => '</section>',
    'before_title'  => '<h2 class="widget-title">',
    'after_title'   => '</h2>',
     ) );
     }
     add_action( 'widgets_init', 'dwqa_theme_register_sidebar' );

     function remove_main_sidebar_dwqa_question(){
     if ( is_singular('dwqa-question') ){
     unregister_sidebar( 'Main Sidebar' );
     }
     }
     add_action( 'widget_init', 'remove_main_sidebar_dwqa_question' );

page.php 中的代码:

    <?php if ( is_singular('dwqa-question') ): ?>
    <?php dynamic_sidebar('dqwa') ?>
    <?php endif; ?>

下面是输出截图:

single question page

我认为您不需要每次都 de-register 主侧边栏,渲染输出需要时间。只需将代码写在siderbar.php中即可。在这里,我将您的一些代码与。

<?php 
        if ( is_singular('dwqa-question') )
        { 
            dynamic_sidebar('dqwa');
        }
        else
        {
          /** FOR OTHER THAN DWQA ***/
          dynamic_sidebar('main-sidebar'); //I might be wrong on ID
        }//end if 
 ?>

希望对你有用