1

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?

2

2 Answers 2

2

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

0
0

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

1
  • I think that too, but I make a plagin and need dinamyc dropdown Jun 20, 2011 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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