您可以使用standard WordPress函数,获取已格式化的下拉列表,并立即解决这两个问题。
像这样:
define( \'WP_USE_THEMES\', false );
require( \'./wp-load.php\' );
wp_dropdown_categories(
array(
\'child_of\' => 0,
\'class\' => \'postform\',
\'depth\' => 0,
\'echo\' => 1,
\'exclude\' => \'\',
\'hide_empty\' => false,
\'hide_if_empty\' => false,
\'hierarchical\' => true,
\'id\' => \'\',
\'name\' => \'cat-dropdown\',
\'order\' => \'ASC\',
\'orderby\' => \'name\',
\'selected\' => 0,
\'show_count\' => 0,
\'show_option_all\' => \'\',
\'show_option_none\' => __(\'None\'),
\'tab_index\' => 0,
\'taxonomy\' => \'category\',
)
);
输出:
<select name=\'cat-dropdown\' id=\'cat-dropdown\' class=\'postform\' >
<option value=\'-1\'>None</option>
<option class="level-0" value="2">Other</option>
<option class="level-0" value="1">Uncategorized</option>
<option class="level-1" value="4"> Other</option>
<option class="level-2" value="5"> Other</option>
</select>
对于默认类别: