dongyo1818 2018-04-25 18:50
浏览 178
已采纳

变量$ term_id不会出现在wordpress的get_term_meta函数中

I would like to show values from custom fields in my Post Categories in Wordpress. To do this, I have used some code in my functions.php file to add and save these custom fields to the different categories in the Wordpress database. I am running into an issue, when trying to pull the values from the custom fields.

Here's the issue: I am trying to use get_term_meta to pull the term_id variable on WordPress:

echo get_term_meta($term_id, 'site_section_1', true);

However, the variable is not showing up when I do this, but it works when I call an individual $term_id such as:

echo get_term_meta(47, 'site_section_1', true);

My database table looks something like this:

meta id    term_id    meta_key                  meta_value
    1          47     site_section_1            Blog Category
    2          47     site_subsection_1         Events Stories
    3          47     department_1              Events
    4          48     site_section_1            Blog Category
    5          48     site_subsection_1         Communications Stories
    6          48     department_1              Communications

I have added this code to the functions.php file to add custom fields to Post Categories:

function wcr_category_fields($term) {
if (current_filter() == 'category_edit_form_fields') {
    $site_section_1 = get_term_meta($term->term_id, 'site_section_1', true);
    $site_subsection_1 = get_term_meta($term->term_id, 'site_subsection_1', true);
    $department_1 = get_term_meta($term->term_id, 'department_1', true);
    ?>
    <tr class="form-field">
        <th valign="top" scope="row"><label for="term_fields[site_section_1]"><?php _e('Site Section'); ?></label></th>
        <td>
              <input type="text" size="40" value="<?php echo esc_attr($site_section_1); ?>" id="term_fields[site_section_1]" name="term_fields[site_section_1]"><br/>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row"><label for="term_fields[site_subsection_1]"><?php _e('Site Subsection'); ?></label></th>
        <td>
            <input type="text" size="40" value="<?php echo esc_attr($site_subsection_1); ?>" id="term_fields[site_subsection_1]" name="term_fields[site_subsection_1]"><br/>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row"><label for="term_fields[department_1]"><?php _e('Department'); ?></label></th>
        <td>
            <input type="text" size="40" value="<?php echo esc_attr($department_1); ?>" id="term_fields[department_1]" name="term_fields[department_1]"><br/>
        </td>
    </tr>
<?php } elseif (current_filter() == 'category_add_form_fields') {
    ?>
    <div class="form-field">
        <label for="term_fields[site_section_1]"><?php _e('site_section_1'); ?></label>
        <input type="text" size="40" value="" id="term_fields[site_section_1]" name="term_fields[site_section_1]">
    </div>
    <div class="form-field">
        <label for="term_fields[site_subsection_1]"><?php _e('site_subsection_1'); ?></label>
        <input type="text" size="40" value="" id="term_fields[site_subsection_1]" name="term_fields[site_subsection_1]">
    </div>
    <div class="form-field">
        <label for="term_fields[department_1]"><?php _e('department_1'); ?></label>
        <input type="text" size="40" value="" id="term_fields[department_1]" name="term_fields[department_1]">
    </div>
<?php
}
}

add_action('category_add_form_fields', 'wcr_category_fields', 10, 2);
add_action('category_edit_form_fields', 'wcr_category_fields', 10, 2);

function wcr_save_category_fields($term_id) {
    if (!isset($_POST['term_fields'])) {
        return;
    }

    foreach ($_POST['term_fields'] as $key => $value) {
        update_term_meta($term_id, $key, sanitize_text_field($value));
    }
}
add_action('edited_category', 'wcr_save_category_fields', 10, 2);
add_action('create_category', 'wcr_save_category_fields', 10, 2);

Please, let me know, if you need any other information.

  • 写回答

1条回答 默认 最新

  • dongshaoxiong0012 2018-04-27 15:31
    关注

    I figured out, how to do this in case anyone else needs this:

    I didn't define $term_id, and I also needed to attach it with the category. So, I used the get_the_category function to pull the term_id, and then I used that as the variable in the get_term_meta function:

    <?php
    $categories = get_the_category();
        if ( ! empty( $categories ) ) {
            $term_id = $categories[0]->term_id;
            echo $term_id;
            echo get_term_meta($term_id, 'site_section_1', true);
            echo get_term_meta($term_id, 'site_subsection_1', true);
            echo get_term_meta($term_id, 'department', true);
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?