【问题标题】:Add product page title to WooCommerce product description heading and tab title将产品页面标题添加到 WooCommerce 产品描述标题和标签标题
【发布时间】:2022-01-29 16:26:47
【问题描述】:

我试图在 WooCommerce 中实现的是,在单个产品页面的“描述”选项卡上,我试图在“描述”一词之前添加产品页面标题。

这是我当前的 WooCommerce 代码:

defined( 'ABSPATH' ) || exit;

global $post;

$heading = apply_filters( 'woocommerce_product_description_heading', __( 'Description', 'woocommerce' ) ); ?>

<?php if ( $heading ) : ?>
<h2>PRODUCT PAGE TITLE HERE <?php echo esc_html( $heading ); ?></h2>
<?php endif; ?>

<?php the_content(); ?>

然而,这里的问题是:

  • 我必须覆盖模板文件,是否可以通过挂钩?
  • 产品标题不是动态的。

因此,id 喜欢从描述转到黑色耐克鞋描述的选项卡

例子:

有什么建议吗?

【问题讨论】:

    标签: wordpress woocommerce product


    【解决方案1】:

    您可以使用woocommerce_product_{$tab_key}_tab_title 复合过滤器挂钩。 $tab_key 在你的情况下是 description

    使用global $product 和$product-&gt;get_name() 获取产品标题。然后可以将此结果添加到现有字符串中。

    所以你得到:

    function filter_woocommerce_product_description_tab_title( $title ) {
        global $product;
        
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            // Get title and append to existing string
            $title = $product->get_name() . ' ' . $title;
        }
        
        return $title;
    }
    add_filter( 'woocommerce_product_description_tab_title', 'filter_woocommerce_product_description_tab_title', 10, 1 );
    

    可选:要改为更改 WooCommerce 产品描述标题,请使用 woocommerce_product_description_heading 过滤钩子:

    function filter_woocommerce_product_description_heading( $heading ) {
        global $product;
        
        // Is a WC product
        if ( is_a( $product, 'WC_Product' ) ) {
            // Get title and append to existing string
            $heading = $product->get_name() . ' ' . $heading;
        }
    
        return $heading;
    }
    add_filter( 'woocommerce_product_description_heading', 'filter_woocommerce_product_description_heading', 10, 1 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2020-12-07
      • 2021-07-12
      相关资源
      最近更新 更多