wordpress标签添加关键词、描述
文章描述:
wordpress后台标签模块添加字段,新增关键词和描述字段
显示
/* 添加input框 */
add_action( 'post_tag_add_form_fields', 'misha_add_term_fields' );
function misha_add_term_fields( $taxonomy ) {
echo '<div class="form-field">
<label for="tag-keywords">关键词</label>
<input type="text" name="tag-keywords" id="tag-keywords" />
<p>Field keywords may go here.</p>
</div>';
echo '<div class="form-field">
<label for="tag-description">描述</label>
<input type="text" name="tag-description" id="tag-description" />
<p>Field description may go here.</p>
</div>';
}
/* 编辑input框 */
add_action( 'post_tag_edit_form_fields', 'misha_edit_term_fields', 10, 2 );
function misha_edit_term_fields( $term, $taxonomy ) {
$keywords_value = get_term_meta( $term->term_id, 'tag-keywords', true );
echo '<tr class="form-field">
<th>
<label for="tag-keywords">关键词</label>
</th>
<td>
<input name="tag-keywords" id="tag-keywords" type="text" value="' . esc_attr( $keywords_value ) .'" />
<p class="description">Field description may go here.</p>
</td>
</tr>';
$value = get_term_meta( $term->term_id, 'tag-description', true );
echo '<tr class="form-field">
<th>
<label for="tag-description">描述</label>
</th>
<td>
<input name="tag-description" id="tag-description" type="text" value="' . esc_attr( $value ) .'" />
<p class="description">Field description may go here.</p>
</td>
</tr>';
}
/* 保存 */
add_action( 'created_post_tag', 'misha_save_term_fields' );
add_action( 'edited_post_tag', 'misha_save_term_fields' );
function misha_save_term_fields( $term_id ) {
update_term_meta(
$term_id,
'tag-keywords',
sanitize_text_field( $_POST[ 'tag-keywords' ] )
);
update_term_meta(
$term_id,
'tag-description',
sanitize_text_field( $_POST[ 'tag-description' ] )
);
}
使用
functions.php
function get_current_tag_id() {
$current_tag = single_tag_title('', false);//获得当前TAG标签名称
$tags = get_tags();//获得所有TAG标签信息的数组
foreach($tags as $tag) {
if($tag->name == $current_tag) return $tag->term_id; //获得当前TAG标签ID,其中term_id就是tag ID
}
}
tag页面
<?php $str = get_term_meta(get_current_tag_id());echo $str['tag-keywords'][0]?>
<?php $str = get_term_meta(get_current_tag_id());echo $str['tag-description'][0]?>
发布时间:2021/07/02
发表评论