Last updated on September 25, 2023

Add a Custom Sidebar to a Specific Page

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Create pages with custom sidebars.

To add a custom sidebar to a specific page in WordPress, you can use the register_sidebar() function to define the sidebar and then use the is_page() function to conditionally display the sidebar on the desired page.

Here’s an example code snippet that demonstrates how to add a custom sidebar to a specific page:

// Register the custom sidebar
function wpsnippets_register_custom_sidebar() {
    register_sidebar( array(
        'name'          => 'Custom Sidebar',
        'id'            => 'custom-sidebar',
        'description'   => 'This is a custom sidebar for a specific page.',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget'  => '</div>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ) );
}
add_action( 'widgets_init', 'wpsnippets_register_custom_sidebar' );

// Display the custom sidebar on a specific page
function wpsnippets_display_custom_sidebar() {
    if ( is_page( 'your-page-slug' ) ) {
        dynamic_sidebar( 'custom-sidebar' );
    }
}
add_action( 'wp_footer', 'wpsnippets_display_custom_sidebar' );

In this example, we first register a custom sidebar using the register_sidebar() function. We provide a name, ID, and description for the sidebar, as well as the HTML markup for the widget container and title.

Next, we use the is_page() function to check if the current page matches the desired page. If it does, we use the dynamic_sidebar() function to display the custom sidebar.

To use this code snippet, you need to replace 'your-page-slug' with the actual slug of the page where you want to display the custom sidebar. You can find the page slug in the WordPress admin area under “Pages” -> “All Pages”.

This code snippet can be useful when you want to have different sidebars for different pages on your WordPress site. It allows you to add custom content or functionality to specific pages without modifying the theme’s core files.

Examples

Example 1: Adding a custom sidebar to a specific page using a page template

This use case demonstrates how to add a custom sidebar to a specific page by creating a custom page template and registering a new sidebar for that template.

<?php
/*
Template Name: Custom Sidebar Page
*/

get_header();

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        // Display page content
        the_content();
    }
}

get_sidebar( 'custom' );
get_footer();
?>

In this example, we create a new page template called “Custom Sidebar Page” by adding a comment at the top of the file with the template name. Inside the template, we use the get_sidebar() function to display the custom sidebar with the name “custom”. This sidebar can be registered using the register_sidebar() function in the theme’s functions.php file.

Example 2: Adding a custom sidebar to a specific page using a conditional statement

This use case demonstrates how to add a custom sidebar to a specific page by using a conditional statement in the theme’s functions.php file.

<?php
function wpsnippets_custom_sidebar() {
    if ( is_page( 'about' ) ) {
        get_sidebar( 'custom' );
    }
}
add_action( 'wp', 'wpsnippets_custom_sidebar' );
?>

In this example, we define a custom function wpsnippets_custom_sidebar() that checks if the current page is the “About” page using the is_page() function. If it is, we use the get_sidebar() function to display the custom sidebar with the name “custom”. We then hook this function to the wp action using the add_action() function.

Example 3: Adding a custom sidebar to a specific page using a page ID

This use case demonstrates how to add a custom sidebar to a specific page by using the page ID in the theme’s functions.php file.

<?php
function wpsnippets_custom_sidebar() {
    if ( is_page( 42 ) ) {
        get_sidebar( 'custom' );
    }
}
add_action( 'wp', 'wpsnippets_custom_sidebar' );
?>

In this example, we define a custom function wpsnippets_custom_sidebar() that checks if the current page has the ID 42 using the is_page() function. If it does, we use the get_sidebar() function to display the custom sidebar with the name “custom”. We then hook this function to the wp action using the add_action() function.

Last updated on September 25, 2023. Originally posted on October 15, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *