我正在为我的网站使用All in one SEO Pack插件进行Meta Title和Meta Description . 我的网站拥有WooCommerce和All in one SEO包不支持WooCommerce类别页面的元 Headers 和元描述 .
所以我使用下面的代码为管理区域中的WooCommerce类别的Meta Title和Meta Description创建自定义字段 .
function wh_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label>
<input type="text" name="wh_meta_title" id="wh_meta_title">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
</div>
<div class="form-field">
<label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label>
<textarea name="wh_meta_desc" id="wh_meta_desc"></textarea>
<p class="description"><?php _e('Enter a meta description, <= 160 character', 'wh'); ?></p>
</div>
<?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);
$wh_meta_desc = get_term_meta($term_id, 'wh_meta_desc', 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>
<tr class="form-field">
<th scope="row" valign="top"><label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label></th>
<td>
<textarea name="wh_meta_desc" id="wh_meta_desc"><?php echo esc_attr($wh_meta_desc) ? esc_attr($wh_meta_desc) : ''; ?></textarea>
<p class="description"><?php _e('Enter a meta description', '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');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');
update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}
add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
以上代码在管理区域中完美运行 . 但如何在类别页面上显示前端输入的元 Headers 和元描述?
我应该在 functions.php
文件中添加哪些钩子,以便它在前端的类别页面中显示?
3 回答
For Meta Title
For Meta Description
所有代码都在您的活动子主题(或主题)的functions.php文件中 . 或者也可以在任何插件的php文件中 .
Codes is tested and works.
有用的链接:
Adding custom field to product category in WooCommerce
Adding Custom Fields to WooCommerce Product Category
希望这可以帮助!
请检查一下:
谢谢
您可以使用以下钩子在前端显示自定义元 Headers 和元描述 .
您可以随时使用其他挂钩修改元数据的显示,方法是按照template structure