You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建可在Elementor使用的WooCommerce产品标签第二描述短代码

你现有代码中自定义的短代码无法在单品页生效的核心原因是:

  • 原有判断条件is_product_taxonomy()仅在产品标签归档页生效,单品页不满足该条件
  • 单品页面无法直接通过全局查询对象获取产品标签数据,需要先读取当前产品关联的标签列表

可直接使用的完整修复后代码

<?php
/**
 * @snippet       给WooCommerce产品标签添加第二描述+单品页可用短代码
 * @compatible    WooCommerce 3.9+
 */  
 
// 1. 后台添加产品标签时显示第二描述输入框
add_action( 'product_tag_add_form_fields', 'bbloomer_wp_editor_add', 10, 2 );
function bbloomer_wp_editor_add() {
    ?>
    <div class="form-field">
        <label for="seconddesc"><?php echo __( '第二描述', 'woocommerce' ); ?></label>
       
      <?php
      $settings = array(
         'textarea_name' => 'seconddesc',
         'quicktags' => array( 'buttons' => 'em,strong,link' ),
         'tinymce' => array(
            'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
            'theme_advanced_buttons2' => '',
         ),
         'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
      );
      wp_editor( '', 'seconddesc', $settings );
      ?>
        <p class="description"><?php echo __( '可通过短代码插入到单品页/标签归档页任意位置', 'woocommerce' ); ?></p>
    </div>
    <?php
}
 
// 2. 后台编辑产品标签时显示第二描述输入框
add_action( 'product_tag_edit_form_fields', 'bbloomer_wp_editor_edit', 10, 2 );
function bbloomer_wp_editor_edit( $term ) {
    $second_desc = htmlspecialchars_decode( get_woocommerce_term_meta( $term->term_id, 'seconddesc', true ) );
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="second-desc"><?php echo __( '第二描述', 'woocommerce' ); ?></label></th>
        <td>
            <?php
         $settings = array(
            'textarea_name' => 'seconddesc',
            'quicktags' => array( 'buttons' => 'em,strong,link' ),
            'tinymce' => array(
               'theme_advanced_buttons1' => 'bold,italic,strikethrough,separator,bullist,numlist,separator,blockquote,separator,justifyleft,justifycenter,justifyright,separator,link,unlink,separator,undo,redo,separator',
               'theme_advanced_buttons2' => '',
            ),
            'editor_css' => '<style>#wp-excerpt-editor-container .wp-editor-area{height:175px; width:100%;}</style>',
         );
         wp_editor( $second_desc, 'seconddesc', $settings );
         ?>
            <p class="description"><?php echo __( '可通过短代码插入到单品页/标签归档页任意位置', 'woocommerce' ); ?></p>
        </td>
    </tr>
    <?php
}
 
// 3. 保存第二描述字段
add_action( 'edit_term', 'bbloomer_save_wp_editor', 10, 3 );
add_action( 'created_term', 'bbloomer_save_wp_editor', 10, 3 );
function bbloomer_save_wp_editor( $term_id, $tt_id = '', $taxonomy = '' ) {
   if ( isset( $_POST['seconddesc'] ) && 'product_tag' === $taxonomy ) {
      update_woocommerce_term_meta( $term_id, 'seconddesc', esc_attr( $_POST['seconddesc'] ) );
   }
}

// 4. 自定义短代码:支持单品页、标签归档页使用
add_shortcode( 'custom_display_tag_seconddesc', 'custom_display_tag_seconddesc' );
function custom_display_tag_seconddesc($atts) {
    // 支持手动指定标签ID,格式:[custom_display_tag_seconddesc tag_id="12"]
    $atts = shortcode_atts( array(
        'tag_id' => '',
    ), $atts );
    
    $output = '';
    $tag_ids = array();

    // 如果手动指定了标签ID,直接读取
    if (!empty($atts['tag_id'])) {
        $tag_ids = array($atts['tag_id']);
    } 
    // 标签归档页,读取当前标签的第二描述
    else if (is_product_taxonomy() && is_tax('product_tag')) {
        $term = get_queried_object();
        if ($term) $tag_ids = array($term->term_id);
    } 
    // 单品页,读取当前产品关联的所有产品标签
    else if (is_product()) {
        global $product;
        $tag_ids = wp_get_post_terms($product->get_id(), 'product_tag', array('fields' => 'ids'));
    }

    // 输出所有符合条件的标签第二描述
    if (!empty($tag_ids)) {
        foreach ($tag_ids as $tag_id) {
            $second_desc = get_woocommerce_term_meta($tag_id, 'seconddesc', true);
            if (!empty($second_desc)) {
                $output .= '<div class="term-description tag-second-desc">' . wc_format_content(htmlspecialchars_decode($second_desc)) . '</div>';
            }
        }
    }
    return $output;
}

使用方法

  • 把上面的代码粘贴到当前主题的functions.php文件末尾,或者自定义代码片段插件中即可生效
  • Elementor单品页使用:直接拖入短代码组件,输入短代码[custom_display_tag_seconddesc]即可自动显示当前产品所有关联标签的第二描述
  • 如果你只想显示指定标签的第二描述,可使用带参数的短代码:[custom_display_tag_seconddesc tag_id="12"],把12替换为你要显示的产品标签ID即可
  • 如果不需要原来的自动在标签归档页产品列表下方显示第二描述的功能,直接删除原有代码中对应woocommerce_after_shop_loop钩子的注册代码和对应函数即可

内容的提问来源于stack exchange,提问作者eugena

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:36:05