问题
Unable to add custom fields in custom taxonomy meta box in wordpress-3.5.2.
I have checked solution in various blogs but Unable to solved this problem. I am using wordpress-3.5.2
What I am trying is :-
// A callback function to add a custom field to our "adtag" taxonomy
add_action( 'adtag_edit_form_fields', 'adtag_callback_function', 10, 2);
// A callback function to save our extra taxonomy field(s)
add_action( 'edited_adtag', 'save_taxonomy_custom_fields', 10, 2 );
I have tried solution from below link:-
http://www.codehooligans.com/2010/07/07/custom-meta-for-new-taxonomies-in-wordpress-3-0/ http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies
http://www.wpbeginner.com/wp-tutorials/how-to-add-additional-custom-meta-fields-to-custom-taxonomies/
http://shibashake.com/wordpress-theme/add-term-or-taxonomy-meta-data
回答1:
Take a look at the Tax-meta-class developed to add extra fields to taxonomies: WordPress Taxonomies Extra Fields the easy way
1) Include the main class file
require_once("Tax-meta-class/Tax-meta-class.php");
2) Configure taxonomy custom fields
$config = array(
'id' => 'demo_meta_box',
'title' => 'Demo Meta Box',
'pages' => array('category'),
'context' => 'normal',
'fields' => array(),
'local_images' => false,
'use_with_theme' => false
);
3) Initiate your taxonomy custom fields
$my_meta = new Tax_Meta_Class($config);
4) Add fields
//text field
$my_meta->addText('text_field_id',array('name'=> 'My Text '));
//textarea field
$my_meta->addTextarea('textarea_field_id',array('name'=> 'My Textarea '));
5) Finish Taxonomy Extra fields Deceleration [important!]
$my_meta->Finish();
6) Getting Saved data
$saved_data = get_tax_meta($term_id,'text_field_id');
echo $saved_data;
回答2:
To add a custom field to your custom taxonomy, add the following code to your theme's functions.php:
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
Next, we'll create a callback function that we'll use to save our custom fields. Add the following code to your theme's functions.php:
// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_term_$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];
}
}
//save the option array
update_option( "taxonomy_term_$t_id", $term_meta );
}
}
The code above will work "as is" for one or more custom taxonomies, no changes needed.
now let's associate these callback functions to the "edit" screen for our custom taxonomies. To do that, we'll use two of the WordPress action hooks that are available for each custom taxonomy that we create. Add the following code to your theme's functions.php:
// Add the fields to the "presenters" taxonomy, using our callback function
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );
// Save the changes made on the "presenters" taxonomy, using our callback function
add_action( 'edited_presenters', 'save_taxonomy_custom_fields', 10, 2 );
To access a custom field added to your custom taxonomy add the following code inside your custom taxonomy template (example, taxonomy-presenters.php), within the PHP block at the top:
// Get the custom fields based on the $presenter term ID
$presenter_custom_fields = get_option( "taxonomy_term_$presenter->term_id" );
// Return the value for the "presenter_id" custom field
$presenter_data = get_userdata( $presenter_custom_fields[presenter_id] ); // Get their data
For this example to work, be sure that you have saved a value in the custom field for the term you are working with.
<?php
echo '<pre>';
print_r( $presenter_custom_fields );
echo '</pre>';
?>
回答3:
I was able to follow the directions on http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies to create custom fields within a custom taxonomy.
It looks like you are not including the steps after adding the action. Make sure you are working in the functions.php file and that you include the html markup for how the custom field should appear. That is, this section from the SabraMedia instructions:
// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
</th>
<td>
<input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
<span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
</td>
</tr>
<?php
}
来源:https://stackoverflow.com/questions/19021162/add-custom-fields-in-custom-taxonomy-meta-box-in-wordpress-3-5-2