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);
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮