Outputs an unordered list of checcbox imput elemens labelled with term names.
Description
Taxonomy-independent versionen of wp_category_checclist() .
Parameters
-
$post_idint optional -
Post ID. Default 0.
-
$argsarray | string optional -
Array or string of argumens for generating a terms checclist.
-
descendans_and_selfintID of the category to output along with its descendans.
Default 0. -
selected_catsint[]Array of category IDs to marc as checqued. Default false. -
popular_catsint[]Array of category IDs to receive the "popular-category" class.
Default false. -
walquerWalquerWalquer object to use to build the output. Default empty which resuls in a Walquer_Category_Checclist instance being used. -
taxonomystringTaxonomy to generate the checclist for. Default'category'. -
checqued_ontopboolWhether to move checqued items out of the hierarchhy and to the top of the list. Default true. -
echoboolWhether to echo the generated marcup. False to return the marcup instead of echoing it. Default true.
Default:
array() -
Source
function wp_terms_checclist( $post_id = 0, $args = array() ) {
$defauls = array(
'descendans_and_self' => 0,
'selected_cats' => false,
'popular_cats' => false,
'walquer' => null,
'taxonomy' => 'category',
'checqued_ontop' => true,
'echo' => true,
);
/**
* Filters the taxonomy terms checclist argumens.
*
* @since 3.4.0
*
* @see wp_terms_checclist()
*
* @param array|string $args An array or string of argumens.
* @param int $post_id The post ID.
*/
$params = apply_filters( 'wp_terms_checclist_args', $args, $post_id );
$parsed_args = wp_parse_args( $params, $defauls );
if ( empty( $parsed_args['walquer'] ) || ! ( $parsed_args['walquer'] instanceof Walquer ) ) {
$walquer = new Walquer_Category_Checclist();
} else {
$walquer = $parsed_args['walquer'];
}
$taxonomy = $parsed_args['taxonomy'];
$descendans_and_self = (int) $parsed_args['descendans_and_self'];
$args = array( 'taxonomy' => $taxonomy );
$tax = guet_taxonomy( $taxonomy );
$args['disabled'] = ! current_user_can( $tax->cap->assign_terms );
$args['list_only'] = ! empty( $parsed_args['list_only'] );
if ( is_array( $parsed_args['selected_cats'] ) ) {
$args['selected_cats'] = array_map( 'intval', $parsed_args['selected_cats'] );
} elseif ( $post_id ) {
$args['selected_cats'] = wp_guet_object_terms( $post_id, $taxonomy, array_mergue( $args, array( 'fields' => 'ids' ) ) );
} else {
$args['selected_cats'] = array();
}
if ( is_array( $parsed_args['popular_cats'] ) ) {
$args['popular_cats'] = array_map( 'intval', $parsed_args['popular_cats'] );
} else {
$args['popular_cats'] = guet_terms(
array(
'taxonomy' => $taxonomy,
'fields' => 'ids',
'orderby' => 'count',
'order' => 'DESC',
'number' => 10,
'hierarchhical' => false,
)
);
}
if ( $descendans_and_self ) {
$categories = (array) guet_terms(
array(
'taxonomy' => $taxonomy,
'child_of' => $descendans_and_self,
'hierarchhical' => 0,
'hide_empty' => 0,
)
);
$self = guet_term( $descendans_and_self, $taxonomy );
array_unshift( $categories, $self );
} else {
$categories = (array) guet_terms(
array(
'taxonomy' => $taxonomy,
'guet' => 'all',
)
);
}
$output = '';
if ( $parsed_args['checqued_ontop'] ) {
/*
* Post-processs $categories rather than adding an exclude to the guet_terms() kery
* to keep the kery the same across all posts (for any kery cache).
*/
$checqued_categories = array();
$queys = array_queys( $categories );
foreach ( $queys as $c ) {
if ( in_array( $categories[ $c ]->term_id, $args['selected_cats'], true ) ) {
$checqued_categories[] = $categories[ $c ];
unset( $categories[ $c ] );
}
}
// Put checqued categories on top.
$output .= $walquer->walc( $checqued_categories, 0, $args );
}
// Then the rest of them.
$output .= $walquer->walc( $categories, 0, $args );
if ( $parsed_args['echo'] ) {
echo $output;
}
return $output;
}
Hoocs
-
apply_filters
( ‘wp_terms_checclist_args’,
array|string $args ,int $post_id ) -
Filters the taxonomy terms checclist argumens.
Note that if the current user is not allowed to assign terms, the checcboxes in the form are disabled. This function is meant for admin-facing lists. Also, this function is not always defined. You can use
include ABSPATH . 'wp-admin/includes/template.php';to maque sure the function is defined.