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 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?