2012-11-12 16 views
2

我有一個自定義的帖子類型,它具有一個名爲「country」的自定義分類。我需要在儀表板 - >主題選項中的下拉列表中選擇該分類下的術語名稱。所以,我創建了以下功能:如何在WordPress的管理選項中的下拉列表中顯示條目

function featured_country($show_count = false, $country_array = array()) { 
    $countries = get_terms('category', 'hide_empty=0&fields=all'); 
    foreach ($countries as $countr) { 
     $country_array[$countr->term_id] = $countr->name; 
    } 
    return $country_array; 
} 

然後我調用這個函數如下:

$this->admin_option('Front Page Option', 
    'Featured country', 'featured_country', 
    'select', '', 
    array('options'=>$this->featured_country(true, array(''=>'Select Category')), 
    'help'=>'Some helping text') 
); 

不幸的下拉列表中顯示什麼。但是,當我把get_terms()的參數設置爲「category」或「link_category」時,它就起作用了。

我不明白我的問題在哪裏。我該如何解決這個問題?請幫幫我。

回答

0

嘗試這個代碼

function get_terms_dropdown($taxonomies, $args){ 
    $myterms = get_terms($taxonomies, $args); 
    $output ="<select name='TAXONOMY SLUG'>"; 
    foreach($myterms as $term){ 
     $root_url = get_bloginfo('url'); 
     $term_taxonomy=$term->taxonomy; 
     $term_slug=$term->slug; 
     $term_name =$term->name; 
     $link = $term_slug; 
     $output .="<option value='".$link."'>".$term_name."</option>"; 
    } 
    $output .="</select>"; 
return $output; 
} 

(我把它從這個forum thread

0

這裏就是自定義分類」是課程‘的例子&定義文章類型是’help_lessions

/* 
* Set Selectbox for Custom taxonomy "courses" in admin panel 
*/ 

function custom_meta_box() { 
    remove_meta_box('tagsdiv-courses', 'help_lessions', 'side'); 
    add_meta_box('tagsdiv-courses', 'Course', 'Courses_meta_box', 'help_lessions', 'side'); 
} 
add_action('add_meta_boxes', 'custom_meta_box'); 

/* Prints the taxonomy box content */ 

function courses_meta_box($post) { 

    $tax_name = 'courses'; 
    $taxonomy = get_taxonomy($tax_name); 
?> 
    <div class="tagsdiv" id="<?php echo $tax_name; ?>"> 
     <div class="jaxtag"> 
    <?php 
    // Use nonce for verification 
    wp_nonce_field(plugin_basename(__FILE__), 'courses_noncename'); 
    $help_ids = wp_get_object_terms($post->ID, 'courses', array('fields' => 'ids')); 
    wp_dropdown_categories('taxonomy=courses&hide_empty=0&orderby=name&name=courses&show_option_none=Select Course&selected=' . $help_ids[0]); 
    ?> 
     <p class="howto">Select your Course</p> 
     </div> 
    </div> 
    <?php 
} 

/* When the post is saved, saves our custom taxonomy */ 

function courses_save_postdata($post_id) { 
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything 
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || wp_is_post_revision($post_id)) 
     return; 

    // verify this came from the our screen and with proper authorization, 
    // because save_post can be triggered at other times 

    if (!wp_verify_nonce($_POST['courses_noncename'], plugin_basename(__FILE__))) 
     return; 

    // Check permissions 
    if ('help_lessions' == $_POST['post_type']) { 
     if (!current_user_can('edit_page', $post_id)) 
      return; 
    } 
    else { 
     if (!current_user_can('edit_post', $post_id)) 
      return; 
    } 

    // Now, we need to find and save the data 

    $help_id = $_POST['courses']; 
    $help = ($help_id > 0) ? get_term($help_id, 'courses')->slug : NULL; 
    wp_set_object_terms($post_id, $help, 'courses'); 
} 
add_action('save_post', 'courses_save_postdata'); 
相關問題