WordPress 主题/插件开发 自定义文章分类 筛选功能 图文教程

前面我们介绍了WordPress开发注册自定义文章的一些方法

下面我们教大家如何在WordPress后台edit.php的自定义文章列表中筛选自定义文章的分类,代码段如下:

function sites_posts_taxonomy_filter() {
	global $typenow; 
	if( $typenow == 'sites' ){ //指定post_type类型
		$taxonomy_names = array('favorites', );//指定分类taxonomy
		foreach ($taxonomy_names as $single_taxonomy) {
			$current_taxonomy = isset( $_GET[$single_taxonomy] ) ? $_GET[$single_taxonomy] : '';
			$taxonomy_object = get_taxonomy( $single_taxonomy );
			$taxonomy_name = strtolower( $taxonomy_object->labels->name );
			$taxonomy_terms = get_terms( $single_taxonomy );
			if(count($taxonomy_terms) > 0) {
				echo "<select name='$single_taxonomy' id='$single_taxonomy' class='postform'>";
				echo "<option value=''>All $taxonomy_name</option>";
				foreach ($taxonomy_terms as $single_term) {
					echo '<option value='. $single_term->slug, $current_taxonomy == $single_term->slug ? ' selected="selected"' : '','>' . $single_term->name .' (' . $single_term->count .')</option>'; 
				}
				echo "</select>";
			}
		}
	}
}
 
add_action( 'restrict_manage_posts', 'sites_posts_taxonomy_filter' );

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注