我的自定义 Post 显示在编辑类别中,但未显示在添加新类别中?代码如下
My Custom Post is show in the Edit Category but Not Display in the Add New Category?Code below
在管理面板的自定义字段中,在编辑类别中显示是这样的吗?
Edit Category Screenshot
但在管理面板自定义字段中未显示在添加新类别中。
Add New Category Screenshot
function extra_edit_tax_fields($tag) {
$t_id = $tag->term_id;
$term_meta = get_option("taxonomy_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Icon Field'); ?></label></th>
<td>
<select style="font-family: 'FontAwesome', Arial;" name="term_meta[img]" id="term_meta[img]" >
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-user-circle') { ?> selected <?php } ?> value="fa-user-circle"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-desktop') { ?> selected <?php } ?> value="fa-desktop"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-indent') { ?> selected <?php } ?> value="fa-indent"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-wifi') { ?> selected <?php } ?> value="fa-wifi"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-search') { ?> selected <?php } ?> value="fa-search"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-ban') { ?> selected <?php } ?> value="fa-ban"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-chevron-circle-right') { ?> selected <?php } ?> value="fa-chevron-circle-right"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-cloud-download') { ?> selected <?php } ?> value="fa-cloud-download"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-cog') { ?> selected <?php } ?> value="fa-cog"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-gg-circle') { ?> selected <?php } ?> value="fa-gg-circle"></option>
</select>
<p class="description"><?php _e('Select the icon '); ?></p>
</td>
</tr>
<?php
}
add_action('category_edit_form_fields', 'extra_edit_tax_fields', 10, 2);
function extra_add_tax_fields($tag) {
$t_id = $tag->term_id;
$term_meta = get_option("taxonomy_$t_id");
}
add_action('category_add_form_fields', 'extra_add_tax_fields', 10, 2);
function save_extra_taxonomy_fields($term_id) {
if (isset($_POST['term_meta'])) {
$t_id = $term_id;
$term_meta = get_option("taxonomy_$t_id");
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if(isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}}
update_option("taxonomy_$t_id", $term_meta);
}}
add_action('edited_category', 'save_extra_taxonomy_fields', 10, 2);
add_action('create_category', 'save_extra_taxonomy_fields', 10, 2);
要使 metabox 也出现在新类别表单中,您需要将函数挂接到 category_add_form_fields
操作以及编辑操作。更新代码以添加下面的第二行会使该框出现在我的测试安装中。
add_action('category_edit_form_fields', 'extra_edit_tax_fields', 10, 2);
add_action('category_add_form_fields', 'extra_edit_tax_fields', 10, 2);
https://developer.wordpress.org/reference/hooks/taxonomy_add_form_fields/
在管理面板的自定义字段中,在编辑类别中显示是这样的吗? Edit Category Screenshot 但在管理面板自定义字段中未显示在添加新类别中。 Add New Category Screenshot
function extra_edit_tax_fields($tag) {
$t_id = $tag->term_id;
$term_meta = get_option("taxonomy_$t_id");
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Icon Field'); ?></label></th>
<td>
<select style="font-family: 'FontAwesome', Arial;" name="term_meta[img]" id="term_meta[img]" >
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-user-circle') { ?> selected <?php } ?> value="fa-user-circle"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-desktop') { ?> selected <?php } ?> value="fa-desktop"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-indent') { ?> selected <?php } ?> value="fa-indent"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-wifi') { ?> selected <?php } ?> value="fa-wifi"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-search') { ?> selected <?php } ?> value="fa-search"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-ban') { ?> selected <?php } ?> value="fa-ban"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-chevron-circle-right') { ?> selected <?php } ?> value="fa-chevron-circle-right"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-cloud-download') { ?> selected <?php } ?> value="fa-cloud-download"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-cog') { ?> selected <?php } ?> value="fa-cog"></option>
<option style="font-family: 'FontAwesome', Arial;" <?php if (esc_attr($term_meta['img']) == 'fa-gg-circle') { ?> selected <?php } ?> value="fa-gg-circle"></option>
</select>
<p class="description"><?php _e('Select the icon '); ?></p>
</td>
</tr>
<?php
}
add_action('category_edit_form_fields', 'extra_edit_tax_fields', 10, 2);
function extra_add_tax_fields($tag) {
$t_id = $tag->term_id;
$term_meta = get_option("taxonomy_$t_id");
}
add_action('category_add_form_fields', 'extra_add_tax_fields', 10, 2);
function save_extra_taxonomy_fields($term_id) {
if (isset($_POST['term_meta'])) {
$t_id = $term_id;
$term_meta = get_option("taxonomy_$t_id");
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if(isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}}
update_option("taxonomy_$t_id", $term_meta);
}}
add_action('edited_category', 'save_extra_taxonomy_fields', 10, 2);
add_action('create_category', 'save_extra_taxonomy_fields', 10, 2);
要使 metabox 也出现在新类别表单中,您需要将函数挂接到 category_add_form_fields
操作以及编辑操作。更新代码以添加下面的第二行会使该框出现在我的测试安装中。
add_action('category_edit_form_fields', 'extra_edit_tax_fields', 10, 2);
add_action('category_add_form_fields', 'extra_edit_tax_fields', 10, 2);
https://developer.wordpress.org/reference/hooks/taxonomy_add_form_fields/