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' );
My Answer:
To display the widget only on the WooCommerce shop page, you can use conditional tags in your sidebar.php file. Here's how you can modify your sidebar.php file:
php
if ( is_shop() && is_active_sidebar( 'shop' ) ) {
?>
} elseif ( is_active_sidebar( 'sidebar-1' ) ) {
?>
}
?>
This code checks if the current page is the WooCommerce shop page using the `is_shop()` function and then displays the 'shop' sidebar if it is active. Otherwise, it falls back to displaying the 'sidebar-1' sidebar.
Make sure to update your functions.php file to register the 'shop' sidebar correctly as you have done in your provided code.
After making these changes, the widget should only appear on the WooCommerce shop page.
Rate this post
3 of 5 based on 9051 votesComments