Site icon T4Tutorials.com

How to show different sidebars for post and page? WordPress

 

Step 1:

write the following code in functions.php

different sidebars for wp post and page
different sidebars for wp post and page
function register_custom_sidebars() {
    // Register a sidebar for posts
    register_sidebar( array(
        'name' => __( 'Post Sidebar', 'textdomain' ),
        'id' => 'post-sidebar',
        'description' => __( 'Sidebar for posts', 'textdomain' ),
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    
    // Register a sidebar for pages
    register_sidebar( array(
        'name' => __( 'Page Sidebar', 'textdomain' ),
        'id' => 'page-sidebar',
        'description' => __( 'Sidebar for pages', 'textdomain' ),
        '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', 'register_custom_sidebars' );

Step 2:

write the following code in sidebar.php

how to show different sidebars for post and page
how to show different sidebars for post and page

 

if ( is_single() && get_post_type() == 'post' ) {
    dynamic_sidebar( 'post-sidebar' );
} elseif ( is_page() ) {
    dynamic_sidebar( 'page-sidebar' );
} else {
    dynamic_sidebar( 'default-sidebar' );
}

Step 3:

Go to Appearance – > widgets and select the post sidebar and page sidebar for your desired action.

show categories only in wp post but not on page
show categories only in wp post but not on page

 

Exit mobile version