在下面的代码中,我在WordPress钩子中插入了一个php块"shop_content".在输出中,插入的php块显示在woocommerce_single_product_summary挂钩之前,这是理所当然的.因此,"shop_content"出现在h1标题之前.我的目标是让标题位于shop_content之上,实质上是将定制的html添加到WordPress钩子中.我想知道是否有一种方法可以重新安排这个钩子,而不用求助于jQuery来重新排序div?我把这hook guide元当做参考.

        <div class="<?php echo esc_attr($ozark_product_summary_class) ?>">
            <div class="summary entry-summary gs-product-summary">
                <?php
                /**
                 * Product Title
                 */
                if (ozark_inherit_option('general_title', 'general_title_product', '1') == '2') {
                    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_title', 5);
                }?>

                <?php if ( is_active_sidebar( 'shop_content' ) ) : ?>
                    <div id="shop_description" class="shop-description widget-area" role="complementary">
                        <?php dynamic_sidebar( 'shop_content' ); ?>
                    </div>
                <?php endif; ?>

                <?php
                /**
                 * Hook: woocommerce_single_product_summary.
                 *
                 * @hooked woocommerce_template_single_title - 5
                 * @hooked woocommerce_template_single_rating - 10
                 * @hooked woocommerce_template_single_price - 10
                 * @hooked woocommerce_template_single_excerpt - 20
                 * @hooked woocommerce_template_single_add_to_cart - 30
                 * @hooked woocommerce_template_single_meta - 40
                 * @hooked woocommerce_template_single_sharing - 50
                 * @hooked WC_Structured_Data::generate_product_data() - 60
                 */

                // remove product short description 
                remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
                remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
                add_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 6);

                do_action('woocommerce_single_product_summary');
                ?>
                <?php if (get_theme_mod('product_share', '2') == '1') : ?>
                    <?php get_template_part('templates/extra/share') ?>
                <?php endif; ?>
            </div>
        </div>

推荐答案

你在制造混乱…进行定制时:

  1. 首先,try 使用可用的挂钩(what you are not doing).
  2. 然后,如果您没有其他 Select ,您可以覆盖WooCommerce模板.

Important:您不能对模板本身使用add_action()remove_action().

对于remove_action()add_action(),您可以重新排列输出,还可以通过您的子主题函数.php文件插入定制代码.

因此,可以try 以下几种方法:

add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary_start', 3 );
function custom_single_product_summary_start(){
    if ( ozark_inherit_option('general_title', 'general_title_product', '1') == '2' ) {
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
    }
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10);
    remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );

    add_action('woocommerce_single_product_summary', 'woocommerce_template_single_rating', 6);
    add_action( 'woocommerce_single_product_summary', 'custom_short_description', 20 );
    add_action( 'woocommerce_single_product_summary', 'custom_single_product_summary_end', 100 );
}
function custom_short_description(){
    if ( is_active_sidebar( 'shop_content' ) ) { ?>
        <div id="shop_description" class="shop-description widget-area" role="complementary">
            <?php dynamic_sidebar( 'shop_content' ); ?>
        </div>
    <?php  }
}

function custom_single_product_summary_end(){
    if (get_theme_mod('product_share', '2') == '1') {
        get_template_part('templates/extra/share');
    }
}

代码放在您的子主题的函数.php文件中(或在插件中).

那么您的模板代码将改为:

        <div class="<?php echo esc_attr($ozark_product_summary_class) ?>">
            <div class="summary entry-summary gs-product-summary">
                <?php
                /**
                 * Hook: woocommerce_single_product_summary.
                 *
                 * @hooked woocommerce_template_single_title - 5
                 * @hooked woocommerce_template_single_rating - 10
                 * @hooked woocommerce_template_single_price - 10
                 * @hooked woocommerce_template_single_excerpt - 20
                 * @hooked woocommerce_template_single_add_to_cart - 30
                 * @hooked woocommerce_template_single_meta - 40
                 * @hooked woocommerce_template_single_sharing - 50
                 * @hooked WC_Structured_Data::generate_product_data() - 60
                 */
                do_action('woocommerce_single_product_summary');
                ?>
            </div>
        </div>

相关:WooCommerce action hooks and overriding templates

Php相关问答推荐

有条件的运费率基于购物车小计与WooPayments货币转换

Symfony Api平台:按自定义实体方式排序结果

Laravel Livewire如何在数组值上使用IF、ELSE条件

MULTI UPDATE Laravel为什么只能更新我的第一行

WordPress插件和JSON不是有效的响应错误

将一个情态形式放在细丝v3中的形式中间

Apache只允许index.php-不从根目录工作

在WooCommerce管理中 for each 运输方法设置添加自定义字段

根据WooCommerce中的客户计费国家/地区更改产品价格

在WooCommerce中只允许高价产品的BACS付款

Laravel 10中的Validator类

在PHP项目中安装OpenAI PHP SDK时出现问题

如何解决 Laravel 外键错误?

对 WooCommerce 购物车和 checkout 中显示的运输方式进行排序

在 WordPress 中将自定义帖子类型永久链接设置为 root

如何从 PHP 中的 .txt 文件中列出今天生日的人的姓名?

PHP 中是否有 is_closure() 函数?

判断类别是否存在 Shopware 6

php-http/discovery 插件 1.15.1 是否仍在使用自己的模块 destruct Contao 4.13 中的composer 安装?

如何获取最后一条消息属于 Laravel 中特定用户的对话列表