如何在选项中显示自定义帖子标签?

时间:2011-06-17 作者:Bobrov_Alexey

我有以下代码:

      $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>
我制作自定义帖子

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\');
在下拉显示中"interior_articles", 但我需要展示"Interior Design Articles"

如何在选项中显示cutom post标签?

2 个回复
最合适的回答,由SO网友:Daniel Sachs 整理而成

你只需要通过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>
请在此处阅读更多信息:http://codex.wordpress.org/Function_Reference/get_post_type_object

SO网友:MartinJJ

有一个解决方案:

<?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>\';  
     ?>  
可能不是理想的选项,但适用于我的设置

结束

相关推荐

创建自定义wp_Dropdown_Categories

我已经为此工作了很多天了。我想将类别分配给作者。我在google上找到了一些提示和插件,但不适用于Wordpress 3.1。我只是想出了我自己的主意。作为管理员,我将为作者创建一个类别,然后在其各自的概要文件元字段中定义或放置类别slug名称。我正在使用自定义的帖子类型名称“networks and taxonomy=blogs”现在,我试图在wp下拉类别中只包含概要文件元字段值(我上面说过)作为默认值,并将其隐藏在我的自定义发布表单中。当我回显时,cat ID和名称是正确的,但它不包括在下拉列表中。有