it-swarm.cn

将自定义字段添加到分类输入:面板

我希望实现一些我认为很容易实现的东西,但似乎没有真正的例子可以实现我的需求。

基本上我有一个自定义帖子类型的“文章”这个自定义的帖子类型我有一个新的分类,我已经注册创建,以便可以添加源出版物。

我的目标是能够通过此自定义帖子类型添加文章,然后从术语列表中选择适用的“源出版物”,以便我可以显示文章的来源。

现在,我遇到的问题似乎很简单......我所要做的就是在页面中添加一些额外的字段,您可以在其中输入每个分类术语。在这种情况下,我想为“URL”添加一个字段,并为每个源包含一个图像,以便我可以添加一个徽标。

所以这里的问题是......我将如何为每个术语添加额外的字段?

我假设如果wordpress不能天真地允许这种功能,那么“描述”字段可以用作一种自定义字段区域,从而可以将数据存储在那里。

然后我尝试将数据提取出来并显示出来。

我可以自定义分类列标题,以防任何人感兴趣,就像可以为自定义帖子类型修改列一样:

// CUSTOM TAXONOMY COLUMNS FOR CONTENT SOURCES
   add_filter("manage_edit-content_sources_columns", 'content_sources_columns');    
   function content_sources_columns($content_sources_columns) {
    $new_columns = array(
        'cb' => '<input type="checkbox" />',
        'name' => __('Name'),
//      'source_image' => '',
        'description' => __('URL'),
        'slug' => __('Slug'),
        'posts' => __('Posts')
        );
    return $new_columns;
   }
4
NetConstructor.com

@ NetConstructor.com

我上个月为某人写过这篇文章,它可能会解决你所寻找的问题。这是您要修改的示例,而不是完整的即用型解决方案:

<?php
/*
 * Example code showing how to hook WordPress to add fields to the taxonomny term edit screen.
 * 
 * This example is meant to show how, not to be a drop in example.
 *
 * This example was written in response to this question:
 *
 *    http://lists.automattic.com/pipermail/wp-hackers/2010-August/033671.html
 *
 * By:
 *
 *    Mike Schinkel (http://mikeschinkel.com/custom-wordpress-plugins/)
 *
 * NOTE:
 *
 *    This could easily become a plugin if it were fleshed out.
 *    A class with static methods was used to minimize the variables & functions added to the global namespace.
 *    wp_options was uses with one option be tax/term instead of via a serialize array because it aids in retrival
 *    if there get to be a large number of tax/terms types. A taxonomy/term meta would be the prefered but WordPress
 *    does not have one.
 *
 * This example is licensed GPLv2.
 *
 */

// These are helper functions you can use elsewhere to access this info
function get_taxonomy_term_type($taxonomy,$term_id) {
  return get_option("_term_type_{$taxonomy}_{$term->term_id}");
}
function update_taxonomy_term_type($taxonomy,$term_id,$value) {
  update_option("_term_type_{$taxonomy}_{$term_id}",$value);
}

//This initializes the class.
TaxonomyTermTypes::on_load();

//This should be called in your own code. This example uses two taxonomies: 'region' & 'opportunity'
TaxonomyTermTypes::register_taxonomy(array('region','opportunity'));

class TaxonomyTermTypes {
  //This initializes the hooks to allow saving of the
  static function on_load() {
    add_action('created_term',array(__CLASS__,'term_type_update'),10,3);
    add_action('edit_term',array(__CLASS__,'term_type_update'),10,3);
  }
  //This initializes the hooks to allow adding the dropdown to the form fields
  static function register_taxonomy($taxonomy) {
    if (!is_array($taxonomy))
      $taxonomy = array($taxonomy);
    foreach($taxonomy as $tax_name) {
      add_action("{$tax_name}_add_form_fields",array(__CLASS__,"add_form_fields"));
      add_action("{$tax_name}_edit_form_fields",array(__CLASS__,"edit_form_fields"),10,2);
    }
  }
  // This displays the selections. Edit it to retrieve
  static function add_form_fields($taxonomy) {
    echo "Type " . self::get_select_html('text');
  }
  // This displays the selections. Edit it to retrieve your own terms however you retrieve them.
  static function get_select_html($selected) {
    $selected_attr = array('text'=>'','user'=>'','date'=>'','etc'=>'');
    $selected_attr[$selected] = ' selected="selected"';
    $html =<<<HTML
<select id="tag-type" name="tag-type">
  <option value="text"{$selected_attr['text']}>Text</option>
  <option value="user"{$selected_attr['user']}>User</option>
  <option value="date"{$selected_attr['date']}>Date</option>
  <option value="etc" {$selected_attr['etc']}>Etc.</option>
</select>
HTML;
    return $html;
  }
    // This a table row with the drop down for an edit screen
    static function edit_form_fields($term, $taxonomy) {
    $selected = get_option("_term_type_{$taxonomy}_{$term->term_id}");
    $select = self::get_select_html($selected);
    $html =<<<HTML
<tr class="form-field form-required">
  <th scope="row" valign="top"><label for="tag-type">Type</label></th>
  <td>$select</td>
</tr>
HTML;
    echo $html;
  }
  // These hooks are called after adding and editing to save $_POST['tag-term']
  static function term_type_update($term_id, $tt_id, $taxonomy) {
    if (isset($_POST['tag-type'])) {
      update_taxonomy_term_type($taxonomy,$term_id,$_POST['tag-type']);
    }
  }
}

希望能帮助到你。

5
MikeSchinkel

我认为在选项表中保存自定义分类法元/额外字段/自定义字段会破坏您的网站性能。就像你的网站重量很大(很多访问者,大量点击),然后对于大量的自定义分类,选项表将是巨大的。它会增加其他插件的get_option()查询的加载时间,或者我认为这会妨碍性能。我们应该在选项表中保存不必要的东西或增加选项表中的行。

2
Manchumahara

在我寻求可能相同的事情时遇到了这篇文章,并且在遇到这个插件后不久: Ultimate Taxonomy Manager 。我还没有认可它,但我已经在测试环境中尝试过,我认为它可以满足您的需求。检索数据时感觉有点笨拙,但这可能只是我和我对文档的理解。

2
jageo

一种更简单,更简单的方法是:

WordPress Taxonomies Extra Fields the easy way - Bainternet

1
Bainternet

您试图通过添加非分类法来修改分类法,这只会混淆。

相反,您需要在文章帖子类型中添加自定义元框,并将网址和图片网址保存在自定义字段中。然后,您将使用代码在functions.php中添加列,以使用get_meta在相关帖子上查找这些自定义字段

1
Tom J Nowell