【问题标题】:Add custom fields in custom taxonomy meta box in wordpress-3.5.2在 wordpress-3.5.2 的自定义分类元框中添加自定义字段
【发布时间】:2013-10-02 00:05:34
【问题描述】:

无法在 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

【问题讨论】:

    标签: wordpress taxonomy custom-taxonomy


    【解决方案1】:

    看看为向分类法添加额外字段而开发的Tax-meta-classWordPress 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;
    

    【讨论】:

      【解决方案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>';  
      ?>
      

      【讨论】:

        【解决方案3】:

        我能够按照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  
        }  
        

        【讨论】:

        • 非常感谢@Summer,至少你给了我答案。经过大量的研发,我找到了更好的解决方案。我在这里所做的会覆盖自定义分类功能。并编写我们自己的与提供给我们的核心 post_tag 分类法类似的函数。我在那个函数中添加了我的字段。它会给我所有自定义字段,包括自定义分类字段...
        • 在您的解决方案中,它将仅为分类页面创建分类页面。当分类法在编辑或添加页面中使用时,它只给出单个分类法字段。所以我的要求在这里有点不同。我也需要添加或编辑页面中的自定义字段。
        【解决方案4】:

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

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

        • 姓名
        • 蛞蝓

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

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-07-29
          • 1970-01-01
          • 2022-01-25
          • 2014-01-09
          • 2012-02-19
          • 2018-07-15
          • 1970-01-01
          • 2017-08-28
          相关资源
          最近更新 更多