简体   繁体   English

如何正确挂钩自定义我的 WooCommerce 商店页面?

[英]How do I properly hook to customize my WooCommerce shop page?

I am creating a theme from scratch and I have installed WooCommerce.我正在从头开始创建一个主题,并且我已经安装了 WooCommerce。 However, I am trying to customize the product archive page.但是,我正在尝试自定义产品存档页面。 I specifically want to have a banner before any WooCommerce content.我特别想在任何 WooCommerce 内容之前有一个横幅。 The code below is from my functions.php .下面的代码来自我的functions.php

add_action( 'woocommerce_before_main_content', 'shop_banner', 35);

function shop_banner(){
  echo '<h1>Hello</h1>';
}

When I call this hook, nothing happens.当我调用这个钩子时,什么也没有发生。

But when I call the “ woocommerce_before_shop_loop ” hook, it works and displays the content (but this is not where I want the content to display).但是当我调用“ woocommerce_before_shop_loop ”钩子时,它会工作并显示内容(但这不是我想要显示内容的地方)。

I decided to test it and see if the hook worked anywhere else, so I added it to my woocommerce.php file.我决定测试它,看看这个钩子是否在其他地方工作,所以我将它添加到我的woocommerce.php文件中。 Below is the code when I added it.下面是我添加时的代码。

<?php get_header() ;
do_action( 'woocommerce_before_main_content' );?>

    <div class="container woocommerce p-5">

        <?php woocommerce_content(); ?>
    </div>




<?php get_footer() ?>

The content displayed when I did that.我这样做时显示的内容。 But I do not want it to be on every page, only my archive pages.但我不希望它出现在每一页上,只出现在我的存档页面上。 So it seems it is hooking on just fine but it is not wanting to display on the archive-product.php page.所以它看起来很好,但它不想显示在存档-product.php页面上。 The code below is the first part of my archive page, which was copied straight from the woocommerce template folder.下面的代码是我存档页面的第一部分,它是直接从 woocommerce 模板文件夹复制而来的。

/**
 * Hook: woocommerce_before_main_content.
 *
 * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb - 20
 * @hooked WC_Structured_Data::generate_website_data() - 30
 */
do_action( 'woocommerce_before_main_content' );

?>
<header class="woocommerce-products-header">
    <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
        <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
    <?php endif; ?>

    <?php
    /**
     * Hook: woocommerce_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     * @hooked woocommerce_product_archive_description - 10
     */
    do_action( 'woocommerce_archive_description' );
    ?>
</header> 

Any help or thoughts would be appreciated.任何帮助或想法将不胜感激。 Also I have only used WooCommerce for about a week so I am very new to the plugin.另外我只使用了大约一周的 WooCommerce 所以我对插件很陌生。

You can add a condition in your hook.您可以在钩子中添加条件。 Take a look to conditional tags on WooCommerce documentation查看WooCommerce 文档上的条件标签

add_action( 'woocommerce_before_main_content', 'shop_banner', 35);
function shop_banner(){
  if(!is_shop()){
     echo '<h1>Hello</h1>';
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM