I want to add a widget that will only appear on the shop page, but I couldn't get any results.
functions.php
function kucuksun_widgets_init() {
register_sidebar(
array(
'name' => esc_html__( 'Sidebar', 'kucuksun' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Add widgets here.', 'kucuksun' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
),
register_sidebar(
array(
'name' => esc_html__( 'Shop', 'kucuksun' ),
'id' => 'shop',
'description' => esc_html__( 'Add widgets here.', 'kucuksun' ),
'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', 'kucuksun_widgets_init' );
Following my code: sidebar.php
<?php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside>
I'm not exactly sure but I need to print it in
woocommerce.php
dynamic_sidebar( 'shop' );
CodePudding user response:
To create a sidebar with widgets that will only appear on the shop page, you can apply the following simple steps:
- Create a new php file, name it
sidebar-shop.php
and place it in your themes root folder - Copy/paste the code from
sidebar.php
tosidebar-shop.php
- Place a widget in the sidebar, and it should only be visible on the shop page
Explanation: page-{slug}.php
— If no custom template has been assigned, WordPress looks for and uses a specialized template that contains the page’s slug.