尝试wp_terms_checklist()
/ wp_category_checklist
. 它将输出一个名为post_category
.
您可能需要包括source 文件,因为它是在管理文件中定义的。
或使用自定义助行器:
class MyCategoryWalker extends Walker_Category{
public function start_el(&$output, $term, $depth, $args){
$args = wp_parse_args(array(
\'name\' => \'my_category_input\',
\'checked\' => array(),
), $args);
extract($args);
$checked = checked(in_array($term->term_id, $checked));
ob_start(); ?>
<li>
<input type="checkbox" <?php $checked; ?> id="category-<?php print $term->term_id; ?>" name="<?php print $name; ?>[]" value="<?php print $term->term_id; ?>" />
<label for="category-<?php print $term->term_id; ?>">
<?php print esc_attr($term->name); ?>
</label>
<?php // closing LI is added inside end_el
$output .= ob_get_clean();
}
}
使用方法如下:
wp_list_categories(array(
\'walker\' => new MyCategoryWalker(),
\'name\' => \'my_category_input\', // name of the input
\'selected\' => array(2, 5, 10), // checked items (category IDs)
));