在Woocommerce类别页面上显示自定义字段

huangapple go评论58阅读模式
英文:

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-&gt;term_id;

// retrieve the existing value(s) for this meta field.
$wh_meta_title = get_term_meta($term_id, &#39;wh_meta_title&#39;, true);
?&gt;
&lt;tr class=&quot;form-field&quot;&gt;
    &lt;th scope=&quot;row&quot; valign=&quot;top&quot;&gt;&lt;label for=&quot;wh_meta_title&quot;&gt;&lt;?php _e(&#39;Meta Title&#39;, &#39;wh&#39;);  ?&gt;&lt;/label&gt;&lt;/th&gt;
    &lt;td&gt;
        &lt;input type=&quot;text&quot; name=&quot;wh_meta_title&quot; id=&quot;wh_meta_title&quot; value=&quot;&lt;?php echo  esc_attr($wh_meta_title) ? esc_attr($wh_meta_title) : &#39;&#39;; ?&gt;&quot;&gt;
        &lt;p class=&quot;description&quot;&gt;&lt;?php _e(&#39;Enter a meta title, &lt;= 60 character&#39;, &#39;wh&#39;); ?&gt;&lt;/p&gt;
    &lt;/td&gt;
&lt;/tr&gt;
&lt;?php
}

add_action(&#39;product_cat_edit_form_fields&#39;, &#39;wh_taxonomy_edit_meta_field&#39;, 10, 1);

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

$wh_meta_title = filter_input(INPUT_POST, &#39;wh_meta_title&#39;);
update_term_meta($term_id, &#39;wh_meta_title&#39;, $wh_meta_title);
}

add_action(&#39;edited_product_cat&#39;, &#39;wh_save_taxonomy_custom_meta&#39;, 10, 1);

And to display it I have this:

add_action(&#39;wp_footer&#39;, &#39;insert_my_page_schemas&#39;);
function insert_my_page_schemas() {
if ( ! is_product_category() ) { 
return;
}
$wh_meta_title = get_term_meta($term_id, &#39;wh_meta_title&#39;, true);
if ( ! $wh_meta_title ) {
return;
}
echo &#39;&lt;script type=&quot;application/ld+json&quot;&gt;&#39;. $wh_meta_title .&#39;&lt;/script&gt;&#39;;
}

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, &#39;wh_meta_title&#39;, true);

This will work only on a category page, not a post/product within a category.

huangapple
  • 本文由 发表于 2023年3月31日 15:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定