【问题标题】:Variable $term_id doesn't show up in get_term_meta function from wordpress变量 $term_id 没有出现在 wordpress 的 get_term_meta 函数中
【发布时间】:2018-10-06 08:28:41
【问题描述】:

我想在 Wordpress 的帖子类别中显示自定义字段的值。为此,我在 functions.php 文件中使用了一些代码,将这些自定义字段添加并保存到 Wordpress 数据库中的不同类别中。尝试从自定义字段中提取值时遇到问题。

这是问题所在:我正在尝试使用 get_term_meta 来拉取 WordPress 上的 term_id 变量:

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

但是,当我这样做时,变量没有显示出来,但是当我调用个人 $term_id 时它可以工作,例如:

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

我的数据库表如下所示:

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

我已将此代码添加到 functions.php 文件中以将自定义字段添加到帖子类别:

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);

如果您需要任何其他信息,请告诉我。

【问题讨论】:

  • 您在哪里使用了提供的代码?为什么没有定义变量?
  • 嗨@SamvelAleqsanyan 感谢您的回复。我在 header.php 文件中添加了代码。并且数据库表在phpMyAdmin中。
  • 你能提供header.php文件的代码吗(编辑你的问题并在那里添加)?
  • 我正在尝试从数据库中提取 $term_id 变量,所以现在,我在 header.php 文件中与此相关的唯一代码是我在上面放置的 echo 语句。
  • 嗨@SamvelAleqsanyan,非常感谢,你真的很有帮助。我是 php 的初学者,所以当你告诉我需要定义 $term_id 时我意识到了我的错误。

标签: php database wordpress


【解决方案1】:

我想出了如何做到这一点,以防其他人需要这个:

我没有定义$term_id,我还需要将它附加到类别中。所以,我用get_the_category函数拉取term_id,然后我把它作为get_term_meta函数中的变量:

<?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);
    }
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多