2

无法在 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-自定义字段到自定义分类

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

4

4 回答 4

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;
于 2015-02-24T09:47:16.307 回答
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 );  

要访问添加到自定义分类中的自定义字段,请在顶部的 PHP 块中的自定义分类模板(例如 taxonomy-presenters.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>';  
?>
于 2013-12-08T14:08:54.260 回答
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  
}  
于 2013-10-22T16:49:45.433 回答
1

现有分类法的创建/版本,无​​论是否定制,都有两个面板。

第一个为通常由元字段组成的分类法创建新术语的小组

  • 姓名
  • 蛞蝓

第二个面板用于编辑现有术语

要将自定义字段添加到分类术语创建面板,第一个面板,请使用:

<?php
// Key of your custom taxonomy goes here.
// Taxonomy key, must not exceed 32 characters.
$prefix_taxonomy = 'category';
 
/**
 * This will add the custom meta field to the add new term page.
 *
 * @return void
 */
function wporg_prefix_add_meta_fields(){
    ?>
 
    <div class="form-field term-meta-wrap">
        <label for="term_meta[custom_term_meta]">
            <?php esc_html_e( 'Example meta field', 'textdomain' ); ?>
        </label>
        <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="" />
        <p class="description">
            <?php esc_html_e( 'Enter a value for this field.', 'textdomain' ); ?>
        </p>
    </div>
     
    <?php
}
add_action( sprintf( '%s_add_form_fields', $prefix_taxonomy ), 'wporg_prefix_add_meta_fields' );

https://developer.wordpress.org/reference/hooks/taxonomy_edit_form_fields/

要将自定义字段添加到编辑面板,第二个面板,请使用:

<?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  
}  
// Add the fields to the "presenters" taxonomy, using our callback function  
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );

https://sabramedia.com/blog/how-to-add-custom-fields-to-custom-taxonomies

于 2020-12-26T18:42:08.060 回答