Title: Add class for attribute values
Last modified: February 15, 2022

---

# Add class for attribute values

 *  Resolved [cousinr](https://wordpress.org/support/users/cousinr/)
 * (@cousinr)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/)
 * Hi,
    I found this code snippet. which, as I understand it, adds attribute values
   to the field (178-179 rows):
 *     ```
       default:
       echo empty( $product->fields[ $field ] ) ? '&nbsp;' : $product->fields[ $field ]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
       break;
       ```
   
 * How do I add a <span class=” ‘attribute name slug’ + ‘attribute value slug’> 
   for attribute values in a table?
 * For exemple:
 *     ```
       <tr class="pa_blablabla different odd">
           <th>Bla bla bla</th>
       <td class="odd product_9860"> - </td>
       <td class="even product_9992">
           <span class="blablabla-red">Red</span>, <span class="blablabla-green">Green</span>
       </td>
       </tr>
       ```
   
    -  This topic was modified 4 years, 3 months ago by [cousinr](https://wordpress.org/support/users/cousinr/).

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Plugin Support [Andrea Grillo](https://wordpress.org/support/users/agengineering/)
 * (@agengineering)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15370224)
 * Hi there,
 * in which file you find this code?
 *  Thread Starter [cousinr](https://wordpress.org/support/users/cousinr/)
 * (@cousinr)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15377475)
 * Hi there,
    I found this code here: [https://plugins.trac.wordpress.org/browser/yith-woocommerce-compare/trunk/templates/compare.php](https://plugins.trac.wordpress.org/browser/yith-woocommerce-compare/trunk/templates/compare.php)
 *  Plugin Support [Andrea Grillo](https://wordpress.org/support/users/agengineering/)
 * (@agengineering)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15378856)
 * Hi there,
 * ok, now I understand the request. You should try to override the template in 
   the theme and change this code in this way (or something similar):
 * `echo '<span class="attribute-' . $field . '">' . empty( $product->fields[ $field])?'&
   nbsp;' : $product->fields[ $field ] . '</span>';`
 * To override the template on your theme simply copy this file: [https://plugins.trac.wordpress.org/browser/yith-woocommerce-compare/trunk/templates/compare.php](https://plugins.trac.wordpress.org/browser/yith-woocommerce-compare/trunk/templates/compare.php)
   in wp-content/themes/your-theme-folder/woocommerce/compare.php.
 * Let me know if works.
 *  Thread Starter [cousinr](https://wordpress.org/support/users/cousinr/)
 * (@cousinr)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15385821)
 * Hi,
    thanks for the code snippet you shared. But unfortunately this is not what
   I had in mind. Your code helped to achieve this **attribute-blablabla**:
 *     ```
       <tr class="pa_blablabla different odd">
           <th>Bla bla bla</th>
       <td class="odd product_9860"> - </td>
       <td class="even product_9992">
           <span class="attribute-blablabla">Red</span>, <span class="attribute-blablabla">Green</span>
       </td>
       </tr>
       ```
   
 * But I need to get this: **blablabla-red, blablabla-green** and more… I need **‘
   Blablabla’ (from ‘pa_blablabla’) + ‘-‘ + ‘slug value’**.
 *     ```
       <tr class="pa_blablabla different odd">
           <th>Bla bla bla</th>
       <td class="odd product_9860"> - </td>
       <td class="even product_9992">
           <span class="blablabla-red">Red</span>, <span class="blablabla-green">Green</span>
       </td>
       </tr>
       ```
   
 * Thanks
 *  Plugin Support [Pablo Pérez](https://wordpress.org/support/users/pperez001/)
 * (@pperez001)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15403523)
 * Hello,
 * Please use this code instead:
 *     ```
       $attribute = get_term_by( 'name', $product->fields[ $field ], $field, 'ARRAY_A' );
       if ( $attribute ) {
            echo '<span class="' . $field . '-' . $attribute['slug'] . '">' . ( empty( $product->fields[ $field ] ) ? '&nbsp;' : $product->fields[ $field ] ) . '</span>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
       } else {
           echo empty( $product->fields[ $field ] ) ? '&nbsp;' : $product->fields[ $field ]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
       }
       break;
       ```
   
 * So this code checks if it’s an attribute and will get the slug of term, creating
   the span that you requested.
 * Let me know if it works in your site.
 *  [Iván Sosa](https://wordpress.org/support/users/ivansosa/)
 * (@ivansosa)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15426151)
 * Hi there,
 * Since we have not received a reply on your side, we will proceed to set this 
   inquiry as resolved. However, for further assistance, do not hesitate to open
   a new thread.
 * Have a great day!
 *  Thread Starter [cousinr](https://wordpress.org/support/users/cousinr/)
 * (@cousinr)
 * [4 years, 2 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15441271)
 * Hi [@pperez001](https://wordpress.org/support/users/pperez001/),
    I apologize
   for the long absence, our region is very turbulent right now. I have nothing 
   worked out. Could you please write exactly what to replace with your code?
 *  Plugin Support [Pablo Pérez](https://wordpress.org/support/users/pperez001/)
 * (@pperez001)
 * [4 years, 2 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15517440)
 * Hello,
 * You just have to replace the part my colleague sent you:
 * `echo '<span class="attribute-' . $field . '">' . empty( $product->fields[ $field])?'&
   nbsp;' : $product->fields[ $field ] . '</span>';`
 * With the one this other:
 *     ```
       $attribute = get_term_by( 'name', $product->fields[ $field ], $field, 'ARRAY_A' );
       if ( $attribute ) {
            echo '<span class="' . $field . '-' . $attribute['slug'] . '">' . ( empty( $product->fields[ $field ] ) ? '&nbsp;' : $product->fields[ $field ] ) . '</span>';// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
       } else {
           echo empty( $product->fields[ $field ] ) ? '&nbsp;' : $product->fields[ $field ]; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
       }
       break;
       ```
   
 * Let me know you encounter any problem and have a great day.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Add class for attribute values’ is closed to new replies.

 * ![](https://ps.w.org/yith-woocommerce-compare/assets/icon.svg?rev=3042935)
 * [YITH WooCommerce Compare](https://wordpress.org/plugins/yith-woocommerce-compare/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/yith-woocommerce-compare/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/yith-woocommerce-compare/)
 * [Active Topics](https://wordpress.org/support/plugin/yith-woocommerce-compare/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/yith-woocommerce-compare/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/yith-woocommerce-compare/reviews/)

 * 8 replies
 * 4 participants
 * Last reply from: [Pablo Pérez](https://wordpress.org/support/users/pperez001/)
 * Last activity: [4 years, 2 months ago](https://wordpress.org/support/topic/add-class-for-attribute-values/#post-15517440)
 * Status: resolved