wp_popular_terms_checklist

函数


wp_popular_terms_checklist ( $taxonomy, $default_term = 0, $number = 10, $display = true )
参数
  • (string)
    $taxonomy
    Taxonomy to retrieve terms from.
    Required:
  • (int)
    $default_term
    Optional. Not used.
    Required:
  • (int)
    $number
    Optional. Number of terms to retrieve. Default 10.
    Required:
    Default: 10
  • (bool)
    $display
    Optional. Whether to display the list as well. Default true.
    Required:
    Default: true
返回值
  • (int[]) Array of popular term IDs.
定义位置
  • wp-admin/includes/template.php
    , line 209
引入
2.5.0
弃用

Retrieves a list of the most popular terms from the specified taxonomy.

If the `$display` argument is true then the elements for a list of checkbox
“ elements labelled with the names of the selected terms is output.
If the `$post_ID` global is not empty then the terms associated with that
post will be marked as checked.

function wp_popular_terms_checklist( $taxonomy, $default_term = 0, $number = 10, $display = true ) {
	$post = get_post();

	if ( $post && $post->ID ) {
		$checked_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
	} else {
		$checked_terms = array();
	}

	$terms = get_terms(
		array(
			'taxonomy'     => $taxonomy,
			'orderby'      => 'count',
			'order'        => 'DESC',
			'number'       => $number,
			'hierarchical' => false,
		)
	);

	$tax = get_taxonomy( $taxonomy );

	$popular_ids = array();

	foreach ( (array) $terms as $term ) {
		$popular_ids[] = $term->term_id;

		if ( ! $display ) { // Hack for Ajax use.
			continue;
		}

		$id      = "popular-$taxonomy-$term->term_id";
		$checked = in_array( $term->term_id, $checked_terms, true ) ? 'checked="checked"' : '';
		?>