我如何添加一个只会出现在 WooCommerce 商店页面上的小部件

How do i add a widget that will only appear on WooCommerce shop page

我想添加一个只会出现在商店页面上的小部件,但我无法获得任何结果。

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' );

按照我的代码:sidebar.php

<?php

if ( ! is_active_sidebar( 'sidebar-1' ) ) {
    return;
}
?>

<aside id="secondary" class="widget-area">
    <?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside>

我不太确定,但我需要打印出来 woocommerce.phpdynamic_sidebar( 'shop' );

要创建一个仅显示在商店页面上的小工具侧边栏, 您可以应用以下简单步骤:

  1. 创建一个新的 php 文件,将其命名为 sidebar-shop.php 并将其放在您的主题根文件夹中
  2. Copy/paste 从 sidebar.phpsidebar-shop.php
  3. 的代码
  4. 在侧边栏中放置一个小部件,它应该只在商店页面上可见

解释:page-{slug}.php — 如果没有指定自定义模板,WordPress 会查找并使用包含页面 slug 的专用模板。