Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

371
Views
Mostrar el campo personalizado de la categoría del producto después del título del producto del bucle WooCommerce

Creé un campo personalizado en la página de creación/edición de categorías usando este código:

 // Product Cat Create Page function product_cat_new_field_create_page() { ?> <div class="form-field"> <label for="product_cat_singular"><?php _e('Singular Name', 'wh'); ?></label> <input type="text" name="product_cat_singular" id="product_cat_singular"> </div> <?php } // Product Cat Edit Page function product_cat_new_field_edit_page($term) { // Getting term ID $term_id = $term->term_id; // Retrieve the existing value(s) for this meta field. $productCatSingular = get_term_meta($term_id, 'product_cat_singular', true); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="product_cat_singular"><?php _e('Singular Title', 'wh'); ?></label></th> <td> <input type="text" name="product_cat_singular" id="product_cat_singular" value="<?php echo esc_attr($productCatSingular) ? esc_attr($productCatSingular) : ''; ?>"> </td> </tr> <?php } add_action('product_cat_add_form_fields', 'product_cat_new_field_create_page', 10, 1); add_action('product_cat_edit_form_fields', 'product_cat_new_field_edit_page', 10, 1); // Save extra taxonomy fields callback function. function wh_save_taxonomy_custom_meta($term_id) { $productCatSingular = filter_input(INPUT_POST, 'product_cat_singular'); update_term_meta($term_id, 'product_cat_singular', $productCatSingular); } add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1); add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);

Usé este tema para lograr esto: agregar un campo personalizado a la categoría del producto

Estoy usando este código para mostrar el nombre de la categoría predeterminada en el ciclo del producto:

 add_action( 'woocommerce_shop_loop_item_title', 'add_product_cat', 15); function add_product_cat() { global $product; $product_cats = wp_get_post_terms($product->id, 'product_cat'); $count = count($product_cats); echo '<div class="categories">'; foreach($product_cats as $key => $cat) { echo '<span class="category">' . $cat->name . '</span>'; } echo '</div>'; }

¿Cómo puedo organizarlo para que muestre el nombre singular (mi nuevo campo) en lugar del nombre predeterminado?

¡Gracias!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Para mostrar su nuevo campo en lugar del nombre de categoría predeterminado en el ciclo del producto, puede usar:

 function action_woocommerce_shop_loop_item_title() { global $product; // Is a WC product if ( is_a( $product, 'WC_Product' ) ) { // Retrieves product term ids for a taxonomy $product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' ); echo '<div class="categories">'; // Iterate over foreach( $product_cats_ids as $product_cats_id ) { echo '<span class="category">' . get_term_meta( $product_cats_id, 'product_cat_singular', true ) . '</span>'; } echo '</div>'; } } add_action( 'woocommerce_shop_loop_item_title', 'action_woocommerce_shop_loop_item_title', 15 );

Nota: Dado que WooCommerce 3 usa $product->get_id() en lugar de $product->id

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!