Display description before the title in WooCommerce product category archives

你离开我真会死。 提交于 2021-02-19 08:50:53

问题


In WooCommerce, I am trying to display the description before the title in Product category archive pages. But I fail trying to find out what hooks could I use to do it.

The idea was to have a "Flavour Menu" be displayed first, and then the title afterwards, like in this screenshot:

Any track is appreciated.


回答1:


You will need to override the template templates/archive-product.php located in the Woocommerce plugin, copying it to your active theme folder to a woocommerce subfolder (but not in atemplatessubfolder).

Read this official related documentation: Template structure & Overriding templates via a theme

Once done, open / edit it and replace:

?>
<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>
<?php

with the following (we are adding a custom hook before the title):

?>
<header class="woocommerce-products-header">
    <?php 
    /**
     * Custom Hook: woocommerce_custom_archive_description.
     *
     * @hooked woocommerce_taxonomy_archive_description - 10
     */
    do_action( 'woocommerce_custom_archive_description' ); ?>

    <?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_product_archive_description - 10
     */
    do_action( 'woocommerce_archive_description' );
    ?>
</header>
<?php

Then save…

Now you will add the following in the function.php file of your active child theme (or active theme), where we will unhook the product category description from the default hook, to hook it in our new custom hook (before the title):

remove_action( 'woocommerce_archive_description', 'woocommerce_taxonomy_archive_description', 10 );
add_action( 'woocommerce_custom_archive_description', 'woocommerce_taxonomy_archive_description', 10 );

Save, you are done… Tested and works.



来源:https://stackoverflow.com/questions/55735359/display-description-before-the-title-in-woocommerce-product-category-archives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!