我的目标很简单 - - 创建具有自定义Metabox的自定义帖子类型,其中具有简单的分类术语列表,而不是使用标签或复选框。原因是要确保编辑器只能从分类列表中选择一个单词。

经过反复试验后,我终于设法弄清楚了我正在使用WP-Alchemy(http://farinspace.com/wpalchemy-metabox/)的出色Metabox创建工具的方法下拉列表中的术语。

我遇到的问题是,我似乎无法从下拉菜单中获得新的选择来保存所选值。

要明确...重要的是,只有单个术语与此自定义帖子类型中的每个帖子相关联,这意味着每当从下拉菜单中选择不同的值并保存帖子时,就需要确保未经注册。

如前所述,我目前将其显示在下拉列表中的术语列表,并且我还正确显示了可能关联的任何术语。

仅保存新值就是我遇到问题的地方。

从广泛的研究中,“似乎”解决方案涉及使用WordPress“ WP_SET_POST_TERMS”函数,此处进一步解释:http://codex.wordpress.org/function_reference/wp_set_post_terms但是,我不确定如何正确利用它来达到所需的结果。

下面我包括我使用的代码。很可能有一种更好的方法来做到这一点,我将感谢您可能提出的任何建议,还可以确保进行适用的“检查”,以确保未授权的用户不能插入数据。

functions.php文件(显示以下自定义邮政类型和分类法的创建。

//////////////////////////////////////////////////////////////////////////////
// CUSTOM POSTTYPE FOR -- SERVICES  
//////////////////////////////////////////////////////////////////////////////

add_action('init', 'services');
function services() {
 register_post_type('services', array(
  'labels' => array(
   'name'   => __('Services'),
   'singular_label'  => __('Service'),
   'new_item'   => __('Add a Service'),
   'add_new'   => __('Add a Service'),
   'add_new_item'  => __('Add a Service'),
   'edit'   => __('Edit Service'),
   'edit_item'   => __('Edit Service'),
   'view'   => __('View Service'),
   'view_item'   => __('View Service'),
   'search_items'  => __('Search Services'),
   'not_found'   => __('No Services Found'),
   'not_found_in_trash' => __('No Services Found in Trash'),
       'parent_item'  => __('Parent Service'),
   'parent_item_colon' => __('Parent Service:')
   ),
  'can_export'    => true,
  'menu_position'   => 7,
  'public'    => false,
  'show_ui'    => true,
  'publicly_queryable'  => true,
  'hierarchical'   => true,
  'query_var'    => true,
  'capability_type'   => 'post',
  'exclude_from_search'  => false,
  'supports' => array(
   'title',
   'editor',
   'revisions',
   'page-attributes'
   ),
  'rewrite' => array( 
   'slug'   => 'disability-services', 
   'with_front'   => false
   )
 ));
}

这是我正在尝试使用的分类学的地方

// CUSTOM TAXONOMY METABOX POSTTYPE - AGE GROUPS
   register_taxonomy('theme', array('services'), array(
    'hierarchical'          => false, 
    'singular_label'            => 'Age Group', 
    'query_var'                 => 'theme',
    'public'                => true,
    'show_ui'               => true,
    'show_tagcloud'             => false,
    'show_in_nav_menus'             => true,    
    'rewrite'               => array( 'slug' => 'age-groups', 'with_front' => false ),
    'labels' => array(
        'name'          => __( 'Age Groups' ),
        'singular_name'         => __( 'Age Groups' ),
        'search_items'      => __( 'Search Age Groups' ),
        'all_items'             => __( 'All Age Groups' ),
        'parent_item'       => __( 'Parent Age Group' ),
        'parent_item_colon'         => __( 'Parent Age Group:' ),
        'edit_item'             => __( 'Edit Age Group' ), 
        'update_item'       => __( 'Update Age Group' ),
        'add_new_item'      => __( 'Add Age Group' ),
        'new_item_name'         => __( 'New Name of Age Group' ),
        'popular_items'         => __( 'Popular Age Groups' ),
        'separate_items_with_commas'=> __( 'Separate Age Groups with commas' ),
        'add_or_remove_items'   => __( 'Add or remove Age Groups' ),
        'choose_from_most_used' => __( 'Select Popular Age Groups' ), 
        ),
   ));
   wp_insert_term('Kids', 'theme');
   wp_insert_term('Teens', 'theme');
   wp_insert_term('Adults', 'theme');

这是我在函数文件和代码中使用的代码的其余部分,用于创建基于WPAlechemy的Metabox。在此尝试中,我尝试包括'save_filter'=>“ wp_set_post_terms($ post-> id,'theme')”,希望这可以保存适用的数据,但没有。

// CUSTOM METABOX POSTTYPE - SERVICE DETAILS
   $custom_metabox_service_details = new WPAlchemy_MetaBox(array (
    'id'        => '_service_details-metaboxes',        // underscore prefix hides fields from the custom fields area
    'title'     => 'Age Groups',            // title added automatically to the custom metabox
    'types'     => array('services'),           // added only for custom post type "name-of-post-type" can also be "page" or "post"
    'context'   => 'normal',                    // same as above, defaults to "normal" but can use "advanced" or "side"
    'priority'  => 'high',                  // same as above, defaults to "high" but can use "low" as well
    'mode'  => WPALCHEMY_MODE_EXTRACT,
    'save_filter' => "wp_set_post_terms( $post->ID, 'theme' )",
    'template'  => TEMPLATEPATH . '/admin-metabox/service_details-metaboxes.php'  // contents for the meta box
    ));

我还应该注意,WPAlechmey的DiMas刚刚在他的1.3.2版本的GitHub中添加了一些新代码,该版本允许上述代码包括一个

'save_filter' => "custom-function", 

此代码允许您创建一个自定义函数,或者我想调用一个自定义函数,该函数在键入发布按钮时被执行,因此也许这是保存数据的最佳方法?

无论如何,我都使用以下代码来显示自定义Metabox显示显示分类学条款的实际下拉列表。

<?php 
// This function gets called in edit-form-advanced.php
echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="' . wp_create_nonce( 'taxonomy_theme' ) . '" />';

// Get all theme taxonomy terms
$themes = get_terms('theme', 'hide_empty=0'); 
?>

<select name='post_theme' id='post_theme'>
// DISPLAY TERMS AS DROP DOWN OPTIONS
<?php $names = wp_get_object_terms($post->ID, 'theme'); ?>
<option class='theme-option' value='' <?php if (!count($names)) echo "selected";?>>None</option>
<?php foreach ($themes as $theme) {
 if (!is_wp_error($names) && !empty($names) && !strcmp($theme->slug, $names[0]->slug)) 
 echo "<option class='theme-option' value='" . $theme->slug . "' selected>" . $theme->name . "</option>\n"; 
 else
 echo "<option class='theme-option' value='" . $theme->slug . "'>" . $theme->name . "</option>\n"; 
}
?>
</select>

我假设保存数据应该很简单,但我想我对如何实现这一目标感到困惑。

如前所述,如果您还可以提供有关代码的建议,以确保完成任何必要的检查以确保正确保存数据,并且仅是正确授权的人。

必须提前感谢!

有帮助吗?

解决方案

对于任何有兴趣的人,我在另一个帖子上回答了这个问题:

保存分类学条款

许可以下: CC-BY-SA归因
scroll top