wp_category_checklist()获取所有分类,并且已checklist的形式输出

目录

描述

Output an unordered list of checkbox <input> elements labeled with category names. This is essentially a convenience function for simplifying calls to wp_terms_checklist()

wp_category_checklist() 描述

用法

<?php
   wp_category_checklist
$post_id$descendants_and_self$selected_cats,
         
$popular_cats$walker$checked_ontop);
?>

wp_category_checklist() 用法

参数

While strictly speaking all parameters are optional because there are defaults, this function is not so useful without specifying either a $post_id or an array of $selected_cats. But don't try to do both.

$post_id

(integer) (可选) Mark categories associated with this post as checked. $selected_cats must be false, and not an array, if a nonzero value is passed here.

默认值: 0

$descendants_and_self

(integer) (可选) ID of the category to output along with its descendents.

默认值: 0

$selected_cats

(boolean|array) (可选) An array of category IDs to mark as checked. If passing a valid $post_id, this should be set as false.

默认值: false

$popular_cats

(boolean|array) (可选) An array of category IDs that overrides the list of categories receiving the "popular-category" class.

默认值: false

$walker

(object) (可选) Walker object to use to build the output. If empty, an instance of Walker_Category_Checklist will be used.

默认值: null

$checked_ontop

(boolean) (可选) Move checked items out of the hierarchy and to the top of the list.

默认值: true

wp_category_checklist() 参数

示例

Output a list off all categories:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
wp_category_checklist();

Output a the list of a certain category and its descendants:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$category_id = 22;

wp_category_checklist( 0, $category_id );

Mark all categories for a particular post as checked:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$post_id = 45;

wp_category_checklist( $post_id );

Specify an array of categories to preselect instead:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$selected_cats = array( 45, 33, 118 );

wp_category_checklist( 0, 0, $selected_cats );

To override which categories will be marked as popular:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$popular = array( 45, 33, 118 );

wp_category_checklist( 0, 0, false, $popular );

Specify a walker object to use:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$walker = new My_Walker_Category_Checklist;

wp_category_checklist( 0, 0, false, false, $walker );

List the checked categories before the rest:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$selected_cats = array( 45, 33, 118 );
$checked_ontop = true;

wp_category_checklist( 0, 0, $selected_cats, false, null, $checked_ontop );

You may also use any of the parameters in combination, except for $post_id and $selected_cats, which should not be used together.

wp_category_checklist() 示例

源文件

wp_category_checklist() 函数的代码位于 wp-admin/includes/template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Output an unordered list of checkbox input elements labeled with category names.
 *
 * @since 2.5.1
 *
 * @see wp_terms_checklist()
 *
 * @param int    $post_id              Optional. Post to generate a categories checklist for. Default 0.
 *                                     $selected_cats must not be an array. Default 0.
 * @param int    $descendants_and_self Optional. ID of the category to output along with its descendants.
 *                                     Default 0.
 * @param array  $selected_cats        Optional. List of categories to mark as checked. Default false.
 * @param array  $popular_cats         Optional. List of categories to receive the "popular-category" class.
 *                                     Default false.
 * @param object $walker               Optional. Walker object to use to build the output.
 *                                     Default is a Walker_Category_Checklist instance.
 * @param bool   $checked_ontop        Optional. Whether to move checked items out of the hierarchy and to
 *                                     the top of the list. Default true.
 */
function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
	wp_terms_checklist( $post_id, array(
		'taxonomy' => 'category',
		'descendants_and_self' => $descendants_and_self,
		'selected_cats' => $selected_cats,
		'popular_cats' => $popular_cats,
		'walker' => $walker,
		'checked_ontop' => $checked_ontop
	) );
}

wp_category_checklist() 源文件

相关