【问题标题】:Custom field for woocommerce category and displaying in home pagewoocommerce 类别的自定义字段并显示在主页中
【发布时间】:2019-08-29 21:16:57
【问题描述】:

我正在尝试在 woocommerce 类别中创建自定义文本字段并将其显示在带有类别列表的主页中,我能够在类别页面中创建自定义字段但无法在主页中使用类别列表显示它 image for custom field

我可以使用此链接创建自定义字段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);

显示带有缩略图和类别标题的 Woocommerce 类别。

            <?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(); ?> 

如何在主页的 woocommerce 类别中显示自定义字段,并在上面的类别循环中列出 woocommerce 类别

【问题讨论】:

    标签: woocommerce


    【解决方案1】:

    好的,我找到了上述问题的解决方案,适用于手动创建自定义字段和 acf 自定义字段,只需在循环中添加以下代码

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

    回声 $custom_field;

    【讨论】:

      猜你喜欢
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 2015-10-29
      • 2021-10-06
      • 1970-01-01
      相关资源
      最近更新 更多