英文:
Display custom field on Woocommerce category page
问题
我已经为WooCommerce产品类别创建了一个自定义字段,基于这里的一个问题:
//Product Cat Edit page
function wh_taxonomy_edit_meta_field($term) {
// 获取术语ID
$term_id = $term->term_id;
// 检索此元字段的现有值
$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('Meta Title', 'wh'); ?></label>
</th>
<td>
<input type="text" name="wh_meta_title" id="wh_meta_title" value="<?php echo esc_attr($wh_meta_title); ?>">
<p class="description"><?php _e('Enter a meta title, <= 60 characters', 'wh'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);
// 保存额外的分类法字段回调函数
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('wp_footer', 'insert_my_page_schemas');
function insert_my_page_schemas() {
if (!is_product_category()) {
return;
}
$wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
if (!$wh_meta_title) {
return;
}
echo '<script type="application/ld+json">' . $wh_meta_title . '</script>';
}
但是它没有被添加到页脚。我需要更改什么?我尝试了上面的函数,但在页脚中没有显示任何内容,尽管字段已经被创建。
英文:
I have created a custom field for Woocommerce product categories based on a question here with this:
//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('Meta Title', '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 a meta title, <= 60 character', 'wh'); ?></p>
</td>
</tr>
<?php
}
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);
And to display it I have this:
add_action('wp_footer', 'insert_my_page_schemas');
function insert_my_page_schemas() {
if ( ! is_product_category() ) {
return;
}
$wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
if ( ! $wh_meta_title ) {
return;
}
echo '<script type="application/ld+json">'. $wh_meta_title .'</script>';
}
But it is not being added to the footer. What do I need to change?
I tried my functions above and nothing is displayed in the footer, the field is created though.
答案1
得分: 0
$term_id
is not defined in insert_my_page_schemas()
You need to find it.
$term_id = get_queried_object_id();
$wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
This will work only on a category page, not a post/product within a category.
英文:
$term_id
is not defined in insert_my_page_schemas()
You need to find it.
$term_id = get_queried_object_id();
$wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
This will work only on a category page, not a post/product within a category.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论