0

Im trying to create custom text-field in woocommerce category and display it in home-page with category list, im able to create custom field in category page but unable to display it with category listing in home-page image for custom field

i was able create custom field with this link here

            <?php
            //Product Cat Edit page
            function wh_taxonomy_edit_meta_field($term) {

                //getting term ID
                $term_id = $term->term_id;

                // retrieve the existing value(s) for this meta field.
                $wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
                ?>
                <tr class="form-field">
                    <th scope="row" valign="top"><label for="wh_meta_title"><?php _e('Custom Link', 'wh'); ?></label></th>
                    <td>
                        <input type="text" name="wh_meta_title" id="wh_meta_title" value="<?php echo esc_attr($wh_meta_title) ? esc_attr($wh_meta_title) : ''; ?>">
                        <p class="description"><?php _e('Enter your Custom link here', 'wh'); ?></p>
                    </td>
                </tr>

                <?php
            }

            add_action('product_cat_add_form_fields', 'wh_taxonomy_add_new_meta_field', 10, 1);
            add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);

            // Save extra taxonomy fields callback function.
            function wh_save_taxonomy_custom_meta($term_id) {

                $wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');

                update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);

            }

            add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
            add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);

Displaying Woocommerce category with thumbnail and category title.

            <?php
                        // get the current taxonomy term


                $prod_categories = get_terms( 'product_cat', array(
                    'orderby'    => 'name',
                    'order'      => 'ASC',
                    'hide_empty' => 1
                ));
                foreach( $prod_categories as $prod_cat ) :
                    $cat_thumb_id = get_woocommerce_term_meta( $prod_cat->term_id, 'thumbnail_id', true );
                    $cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
                    $term_link = get_term_link( $prod_cat, 'product_cat' );

            ?>

            <div class="col-md-2 text-center">
                <h1><?php echo 'custom_field';?></h1> <!--displaying category custom field here--> 
            <a href="<?php echo $custom; ?>">
                <img src="<?php echo $cat_thumb_url; ?>" alt="" class="w-100 d-block"/>
                <div class="cat-product-title">
                    <h4><?php echo $prod_cat->name; ?></h4>
                </div>
            </a>
            </div>

            <?php endforeach; wp_reset_query(); ?> 

how can i display custom field create in woocommerce category in home page with woocommerce category listing in above category loop

syner
  • 96
  • 8

1 Answers1

0

ok, i find solution for above problem working for both custom field create manually and acf custom field, just add following code in the loop

$category = get_term_by('slug', $prod_cat->name ,'product_cat'); $custom_field = get_field('your_custom_field', $category );

echo $custom_field;

syner
  • 96
  • 8