Php 变量$term_id不';无法在wordpress的get_term_元函数中显示
我想在Wordpress中显示我文章类别中自定义字段的值。为此,我在Php 变量$term_id不';无法在wordpress的get_term_元函数中显示,php,database,wordpress,Php,Database,Wordpress,我想在Wordpress中显示我文章类别中自定义字段的值。为此,我在functions.php文件中使用了一些代码,将这些自定义字段添加并保存到Wordpress数据库的不同类别中。当我试图从自定义字段中提取值时,遇到了一个问题 问题是:我试图使用get\u term\u meta在WordPress上拉取term\u id变量: echo get_term_meta($term_id, 'site_section_1', true); 但是,当我执行此操作时,变量不会显示,但当我调用单个$t
functions.php
文件中使用了一些代码,将这些自定义字段添加并保存到Wordpress数据库的不同类别中。当我试图从自定义字段中提取值时,遇到了一个问题
问题是:我试图使用get\u term\u meta
在WordPress上拉取term\u id
变量:
echo get_term_meta($term_id, 'site_section_1', true);
但是,当我执行此操作时,变量不会显示,但当我调用单个$term\u 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);
功能wcr\u类别\u字段($term){
如果(当前过滤器()='category\u edit\u form\u fields'){
$site\u section\u 1=get\u term\u meta($term->term\u id,'site\u section\u 1',true);
$site\u subsection\u 1=get\u term\u meta($term->term\u id,'site\u subsection\u 1',true);
$department\u 1=get\u term\u meta($term->term\u id,'department\u 1',true);
?>
我想,如果其他人需要,如何做到这一点:
我没有定义$term\u id
,我还需要将其与类别一起附加。因此,我使用get\u the\u the\u category
函数来拉取term\u id
,然后在get\u term\u 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);
}
?>
我想出了在其他人需要时如何做到这一点:
我没有定义$term\u id
,我还需要将其与类别一起附加。因此,我使用get\u the\u the\u category
函数来拉取term\u id
,然后在get\u term\u 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);
}
?>
您在哪里使用了提供的代码?为什么没有定义变量?Hi@SamvelAleqsanyan感谢您的回答。我在header.php文件中添加了代码。数据库表在phpMyAdmin中。您能提供header.php
文件的代码吗(编辑您的问题并添加到那里)?我正试图从数据库中提取$term_id变量,所以现在,我在header.php文件中唯一与此相关的代码是我上面的echo语句。您好@SamvelAleqsanyan,非常感谢,您真的很有帮助。我是php初学者,所以当您告诉我需要定义$term_id时,我意识到了我的错误。您在哪里使用了d代码?为什么没有定义变量?Hi@SamvelAleqsanyan感谢您的回复。我在header.php文件中添加了代码。数据库表在phpMyAdmin中。您能提供header.php
文件的代码吗(编辑您的问题并添加到那里)?我正试图从数据库中提取$term_id变量,所以现在,我在header.php文件中唯一与此相关的代码是我在上面放的echo语句。您好@SamvelAleqsanyan,非常感谢,您真的很有帮助。我是php初学者,所以当您告诉我需要定义$term_id时,我意识到了我的错误。