在wordpress-3.5.2中的自定义分类法元框中添加自定义字段

时间:2013-09-26 06:43:12

标签: wordpress taxonomy custom-taxonomy

无法在wordpress-3.5.2中的自定义分类法元框中添加自定义字段。

我已在各种博客中检查过解决方案,但无法解决此问题。我使用的是wordpress-3.5.2

我正在尝试的是: -

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

我尝试过以下链接的解决方案: -

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

3 个答案:

答案 0 :(得分:3)

查看为在分类法中添加额外字段而开发的 Tax-meta-class WordPress Taxonomies Extra Fields the easy way

1)包括主类文件

require_once("Tax-meta-class/Tax-meta-class.php");

2)配置分类自定义字段

$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)启动分类自定义字段

$my_meta = new Tax_Meta_Class($config);

4)添加字段

//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)完成分类法额外字段减速[重要!]

$my_meta->Finish();

6)获取已保存的数据

$saved_data = get_tax_meta($term_id,'text_field_id');
echo $saved_data;

答案 1 :(得分:2)

要在自定义分类中添加自定义字段,请将以下代码添加到主题的 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  
}  

接下来,我们将创建一个回调函数,用于保存自定义字段。将以下代码添加到主题的 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 );  
    }  
}  

上面的代码将“按原样”用于一个或多个自定义分类,无需进行任何更改。

现在让我们将这些回调函数关联到我们自定义分类法的“编辑”屏幕。为此,我们将使用两个WordPress操作挂钩,这些挂钩可用于我们创建的每个自定义分类。将以下代码添加到主题的 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 );  

访问添加到自定义分类中的自定义字段 在自定义分类模板(例如,taxonomy-presenters.php)中,在顶部的PHP块中添加以下代码:

// 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  

要使此示例正常工作,请确保您已在自定义字段中保存了与您合作的字词的值。

<?php  
    echo '<pre>';  
    print_r( $presenter_custom_fields );  
    echo '</pre>';  
?>

答案 2 :(得分:1)

我能够按照http://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies上的说明在自定义分类中创建自定义字段。

在添加操作后,您似乎没有包含这些步骤。确保您正在使用functions.php文件,并且包含自定义字段应如何显示的html标记。也就是说,本节来自SabraMedia说明:

// 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  
}