php - 在 WooCommerce 中向产品类别和标签添加 2 个自定义字段,但代码会发出通知
问题描述
参考这个问题。在 WooCommerce 中将自定义字段添加到产品类别
我正在尝试这段代码,但它给了我通知。
注意:未定义变量:第 61 行 /home/userdir/public_html/wp-content/themes/theme-child/functions.php 中的 term_id
这是我正在尝试的代码,几乎没有修改。有人可以让我知道我在这里做错了什么吗?
function text_domain_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label>
<textarea name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]"></textarea>
<p class="description"><?php _e('Enter a Text at Bottom, <= 160 character', 'text_domain'); ?></p>
</div>
<div class="form-field">
<label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label>
<textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"></textarea>
<p class="description"><?php _e('Enter Text at Top, <= 160 character', 'text_domain'); ?></p>
</div>
<?php
}
add_action('product_cat_add_form_fields', 'text_domain_taxonomy_add_new_meta_field', 10, 2);
//Product Cat Edit page
function text_domain_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option("taxonomy_" . $term_id);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[wh_meta_title]"><?php _e('Text at Bottom', 'text_domain'); ?></label></th>
<td>
<textarea name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]"><?php echo esc_attr($term_meta['wh_meta_title']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?></textarea>
<p class="description"><?php _e('Text at Bottom, <= 160 character', 'text_domain'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Text at Top', 'text_domain'); ?></label></th>
<td>
<textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"><?php echo esc_attr($term_meta['wh_meta_desc']) ? esc_attr($term_meta['wh_meta_desc']) : ''; ?></textarea>
<p class="description"><?php _e('Text at Top', 'text_domain'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_edit_form_fields', 'text_domain_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta($term_id) {
if (isset($_POST['term_meta'])) {
$term_meta = get_option("taxonomy_" . $term_id);
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_" . $term_id, $term_meta);
}
}
add_action('edited_product_cat', 'save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'save_taxonomy_custom_meta', 10, 2);
$metaArray = get_option('taxonomy_' . $term_id);
echo $productCatMetaTitle = $metaArray['wh_meta_title'];
echo $productCatMetaDesc = $metaArray['wh_meta_desc'];
//Product Tag creation page
function tag_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="term_meta[tg_meta_title]"><?php _e('Text at Bottom', 'text_domain'); ?></label>
<textarea name="term_meta[tg_meta_title]" id="term_meta[tg_meta_title]"></textarea>
<p class="description"><?php _e('Text at Bottom, <= 160 character', 'text_domain'); ?></p>
</div>
<div class="form-field">
<label for="term_meta[tg_meta_desc]"><?php _e('Text at Top', 'text_domain'); ?></label>
<textarea name="term_meta[tg_meta_desc]" id="term_meta[tg_meta_desc]"></textarea>
<p class="description"><?php _e('Text at bottom, <= 160 character', 'text_domain'); ?></p>
</div>
<?php
}
add_action('product_tag_add_form_fields', 'tag_taxonomy_add_new_meta_field', 10, 2);
//Product tag Edit page
function tag_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option("taxonomy_" . $term_id);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[tg_meta_title]"><?php _e('Text at Bottom', 'text_domain'); ?></label></th>
<td>
<textarea name="term_meta[tg_meta_title]" id="term_meta[tg_meta_title]"><?php echo esc_attr($term_meta['tg_meta_title']) ? esc_attr($term_meta['tg_meta_title']) : ''; ?></textarea>
<p class="description"><?php _e('Text will show in bottom, <= 260 character', 'text_domain'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[tg_meta_desc]"><?php _e('Text at Top', 'text_domain'); ?></label></th>
<td>
<textarea name="term_meta[tg_meta_desc]" id="term_meta[tg_meta_desc]"><?php echo esc_attr($term_meta['tg_meta_desc']) ? esc_attr($term_meta['tg_meta_desc']) : ''; ?></textarea>
<p class="description"><?php _e('Text will show at Top', 'text_domain'); ?></p>
</td>
</tr>
<?php
}
add_action('product_tag_edit_form_fields', 'tag_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function tag_save_taxonomy_custom_meta($term_id) {
if (isset($_POST['term_meta'])) {
$term_meta = get_option("taxonomy_" . $term_id);
$tag_keys = array_keys($_POST['term_meta']);
foreach ($tag_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_" . $term_id, $term_meta);
}
}
add_action('edited_product_tag', 'tag_save_taxonomy_custom_meta', 10, 2);
add_action('create_product_tag', 'tag_save_taxonomy_custom_meta', 10, 2);
$metaArray = get_option('taxonomy_' . $term_id);
echo $productTagMetaTitle = $metaArray['tg_meta_title'];
echo $productTagMetaDesc = $metaArray['tg_meta_desc'];
解决方案
删除此代码
$metaArray = get_option('taxonomy_' . $term_id);
echo $productCatMetaTitle = $metaArray['wh_meta_title'];
echo $productCatMetaDesc = $metaArray['wh_meta_desc'];
从代码中的两个地方
推荐阅读
- c - C:有没有办法避免给定行中的代码重复?
- python-3.x - 使用硒粘贴到texbox
- java - 无法将 Spring Boot 应用升级到 Flyway 7.0.0
- java - 如何评估 java 运算符的评估
- java - 未加权二维数组中的最短路径。如何显示 BFS 期间采取的步骤/方向
- json - 如何在 Python 中使用字典字典数组解析 JSON?
- python-3.x - 使用 app-store-scraper 检索 AppStore 应用程序的评论时出错?
- firebase - Flutter - 在将 Firebase 用户写入拥有的用户实例时获取 Firebase 自定义声明
- python - 通过 Pytorch 转换 EMNIST 数据时出错
- python - pandas groupby 提供新的字段名称来聚合列