首页 > 解决方案 > 单页上的 Wordpress 自定义侧边栏

问题描述

我正在尝试在页面中显示自定义侧边栏。该代码适用于自定义帖子类型“新闻”帖子,但不适用于“na-mdia”页面。在页面中显示了默认的侧边栏。

// Custom Sidebar
function prefix_custom_sidebar() {

    register_sidebar( array(
        'name'          => __( 'Custom Sidebar Mídia', 'page-builder-framework' ),
        'id'            => 'custom-sidebar',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h4 class="wpbf-widgettitle">',
        'after_title'   => '</h4>'
    ) );

}
add_action( 'widgets_init', 'prefix_custom_sidebar' );

// Replace the default sidebar with our new custom sidebar on all docs posts
function prefix_do_custom_sidebar( $sidebar ) {
// this statement works for custom post types
    if( is_singular( 'news' ) ) {
        $sidebar ='custom-sidebar';
    }
// this statement NOT works for the page which is displaying the posts, display the default sidebar instead
    elseif ( is_singular( 'na-midia' ) ) {
        $sidebar ='custom-sidebar';
    }

    return $sidebar;

}
add_filter( 'wpbf_do_sidebar', 'prefix_do_custom_sidebar' );

标签: phpwordpress

解决方案


解决了!

elseif ( is_page ( 'na-midia' ) ) {
        $sidebar ='custom-sidebar';
    }


推荐阅读