I added page to widget and my wordpress adds all my pages into widget.

I want add just contact page to widget and remove all other pages from the widget.

How to do that without plugin?

    register_sidebar( array(
    'name'          => esc_html__( 'Sidebar', 'onepress' ),
    'id'            => 'sidebar-1',
    'description'   => '',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget'  => '</aside>',
    'before_title'  => '<h2 class="widget-title">',
    'after_title'   => '</h2>',
   ) );

Follow this step for Only one page add custom sidebar

Step 1: Put this code in your theme function.php file. Register custom sidebar and your custom add_action.

function wpdocs_theme_slug_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Custom Sidebar', 'textdomain' ),
        'id'            => 'sidebar-10',
        'description'   => __( 'Widgets in this area will be shown on all posts and pages.', 'textdomain' ),
        'before_widget' => '<li id="%1$s" class="widget %2$s">',
        'after_widget'  => '</li>',
        'before_title'  => '<h2 class="widgettitle">',
        'after_title'   => '</h2>',
    ) );
add_action( 'widgets_init', 'wpdocs_theme_slug_widgets_init' );

add_action( 'your_custom_hook_name', 'your_function_name' );
function your_function_name( $sidebar_name )
    is_page( 'sample-page' ) && dynamic_sidebar( 'sidebar-10' ); // your sidebar name
    remove_action( current_filter(), __FUNCTION__ );

Step 2: Put this code where you display this widgets

<?php do_action( 'your_custom_hook_name' ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.