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_terms_checklistwp_category_checklistwp_link_category_checklistwp_parse_listwp_clean_themes_cache
引入
2.5.0
弃用
-

wp_popular_terms_checklist: 这个函数用来生成分类法中流行术语列表的HTML输出。

从指定的分类法中检索最受欢迎的术语列表。

如果`$display`参数为真,那么将输出标有所选术语名称的复选框“元素列表。
如果`$post_ID`全局不是空的,那么与该文章相关的术语将被标记为选中。

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"' : '';
		?>

		<li id="<?php echo $id; ?>" class="popular-category">
			<label class="selectit">
				<input id="in-<?php echo $id; ?>" type="checkbox" <?php echo $checked; ?> value="<?php echo (int) $term->term_id; ?>" <?php disabled( ! current_user_can( $tax->cap->assign_terms ) ); ?> />
				<?php
				/** This filter is documented in wp-includes/category-template.php */
				echo esc_html( apply_filters( 'the_category', $term->name, '', '' ) );
				?>
			</label>
		</li>

		<?php
	}
	return $popular_ids;
}

常见问题

FAQs
查看更多 >