Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this code:

      $args=array(
        'public'   => true,
        '_builtin' => false
      );
      ?>
      <select id="<?php echo $this->get_field_id('posttype'); ?>" name="<?php echo $this->get_field_name('posttype'); ?>">
          <?php foreach(get_post_types($args,'names') as $post_type) { ?>
              <option <?php selected( $instance['posttype'], $post_type ); ?> value="<?php echo $post_type; ?>"><?php echo $post_type; ?></option>
          <?php } ?>      
      </select>

I make custom posts

function my_post_type_interior_articles() {
        register_post_type( 'interior_articles',
                    array( 
                    'label' => __('Interior Design Articles'), 
                    'public' => true, 
                    'show_ui' => true,
                    'show_in_nav_menus' => false,
                    'menu_position' => 5,
                    'supports' => array(
                            'title',
                            'custom-fields',
                            'editor',
                            'excerpt',
                            'thumbnail')
                        ) 
                    );
    }

    add_action('init', 'my_post_type_interior_articles');

in dropdown shows "interior_articles", but i need to show "Interior Design Articles"

How to show cutom post labels in options?

share|improve this question
    
is this what you mean? (last block of code) wordpress.stackexchange.com/questions/18159/… –  MartinJJ Jun 17 '11 at 9:19
    
@Martin, I edit post, hope it help to understand what I want –  Bobrov_Alexey Jun 20 '11 at 7:05
add comment

2 Answers

up vote 2 down vote accepted

You just need to pull the Labels via get_post_type_object();

<?php 
$args=array(
  'public'   => true,
  '_builtin' => false
); 
$output = 'names';
$operator = 'and';
$post_types=get_post_types($args,$output,$operator); 
?>
<select id="" name="">

    <?php foreach ($post_types as $post_type ) {      
        $label_obj = get_post_type_object($post_type); 
        $labels = $label_obj->labels->name;
    ?>

        <option <?php selected( $instance['posttype'], $post_type ); ?> value="<?php echo $post_type; ?>"><?php echo $labels; ?></option>

    <?php } ?>

</select>

Read more here: http://codex.wordpress.org/Function_Reference/get_post_type_object

share|improve this answer
    
this is exactly what I want. Thanks ! –  Bobrov_Alexey Jun 21 '11 at 6:12
add comment

Theres a fix for that:

<?php 
          $args=array(
                     'public'   => true,
                     '_builtin' => false
                     ); 
                $output = 'names';
                $operator = 'and';
                $post_types=get_post_types($args,$output,$operator); 

          echo '<select name="customcategory">';
          echo '<option value="interior_articles">Interior Design Articles</option>';
          echo '<option value="another_post_type">Another Post Type</option>';
          echo '<option value="and_yet_another_post_type">And Yet Another Post Type</option>';
          echo '</select>';  
     ?>  

probably not the ideal option but works for my setup

share|improve this answer
    
I think that too, but I make a plagin and need dinamyc dropdown –  Bobrov_Alexey Jun 20 '11 at 7:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.