WordPress获取所有分类列表函数

通过函数get_categories()就可以输出 WordPress 获取所有分类列表。当然也可以使用get_terms(),后面说一下。这两者的区别是get_categories按照文章类型来获取,而get_terms时通过分类法(其他地方也叫分类术语)。

一、get_categories

用法

<?php $categories = get_categories( $args ); ?>

$args默认值

<?php
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
?>

参数说明

type:(字符)post和link 其中link在新版3.0以后已被弃用。

child_of:(整数)仅显示标注了编号的分类的子类。该参数无默认值。使用该参数时应将hide_empty参数设为false

parent:(整数)只显示某个父级分类以及下面的子分类(注:子分类只显示一个层级)。

orderby:(字符)将分类按字母顺序或独有分类编号进行排序。默认为按分类 编号排序包括ID(默认)和Name

order:(字符)为类别排序(升序或降序)。默认升序。可能的值包括asc(默认)和desc

hide_empty:(布尔值)触发显示没有文章的分类。默认值为true(隐藏空类别)。有效的值包括:1(true)和0(false)

hierarchical:(布尔值)将子类作为内部列表项目(父列表项下)的层级关系。默认为true(显示父列表项下的子类)。有效值包括1 (true)和0(false)

exclude:(字符)除去分类列表中一个或多个分类,多个可以用逗号分开,用分类ID号表示

include:(字符)只包含指定分类ID编号的分类。多个可以用逗号分开,用分类ID号表示

number:(字符)将要返回的类别数量

pad_counts:(布尔值)通过子类中的项来计算链接或文章。有效值包括1(true)和0(false),0为默认

taxonomy:(字符)返回一个分类法,这个是wordpress3.0版本后新添加的一个参数。返回的值包括category(默认)和taxonomy(一些新定义的分类名称)

示例

<?php
$args=array(
'orderby' => 'name',
'hide_empty' => false, //显示所有分类
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
?>

二、get_terms

上面的获取分类我个人并不是很喜欢,如果想要获取自定义分类法的分类时,你就会发现无从下手。其实还有一个函数可以获取,那就是get_terms,它可以获取WordPress中所有的分类法,包括你自定义的。

用法:

$terms = get_terms( array( 
    'taxonomy' => 'tax_name',
    'parent'   => 0
) );
//最简单的用法
$terms = get_terms();

示例一,获取所有文章标签post_tag

如果你注意到我之前写的新建自定义分类法,说过一句话,其实文章标签就是用的分类实现的,它和分类是一样的。他的分类法时post_tag,接下来我们就获取一下:

$terms = get_terms( 'post_tag', array(
    'hide_empty' => false,
) );

//4.5版本后新增了这样的写法
$terms = get_terms( array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
) );

示例二,获取所有文章分类category

$terms = get_terms( 'category', array(
    'hide_empty' => false,
) );

//4.5版本后新增了这样的写法
$terms = get_terms( array(
    'taxonomy' => 'category',
    'hide_empty' => false,
) );

一些其他例子:

1、获取文章分类的,参数还比较全

$get_terms_default_attributes = array (
			'taxonomy' => 'category', //empty string(''), false, 0 don't work, and return empty array
			'orderby' => 'name',
			'order' => 'ASC',
			'hide_empty' => true, //can be 1, '1' too
			'include' => 'all', //empty string(''), false, 0 don't work, and return empty array
			'exclude' => 'all', //empty string(''), false, 0 don't work, and return empty array
			'exclude_tree' => 'all', //empty string(''), false, 0 don't work, and return empty array
			'number' => false, //can be 0, '0', '' too
			'offset' => '',
			'fields' => 'all',
			'name' => '',
			'slug' => '',
			'hierarchical' => true, //can be 1, '1' too
			'search' => '',
			'name__like' => '',
			'description__like' => '',
			'pad_counts' => false, //can be 0, '0', '' too
			'get' => '',
			'child_of' => false, //can be 0, '0', '' too
			'childless' => false,
			'cache_domain' => 'core',
			'update_term_meta_cache' => true, //can be 1, '1' too
			'meta_query' => '',
			'meta_key' => array(),
			'meta_value'=> '',
	);

2、一个简单的示例,教你如何遍历显示出结果

$taxonomies = get_terms( array(
	'taxonomy' => 'taxonomy_name',
	'hide_empty' => false
) );

if ( !empty($taxonomies) ) :
	$output = '<select>';
	foreach( $taxonomies as $category ) {
		if( $category->parent == 0 ) {
			$output.= '<optgroup label="'. esc_attr( $category->name ) .'">';
			foreach( $taxonomies as $subcategory ) {
				if($subcategory->parent == $category->term_id) {
				$output.= '<option value="'. esc_attr( $subcategory->term_id ) .'">
					'. esc_html( $subcategory->name ) .'</option>';
				}
			}
			$output.='</optgroup>';
		}
	}
	$output.='</select>';
	echo $output;
endif;

3、两种获取分类的方法

//字符串法
$categories = get_terms( 'category', 'orderby=count&hide_empty=0' );
//数组法
$categories = get_terms( 'category', array(
	'orderby'    => 'count',
	'hide_empty' => 0,
) );

4、获取链接分类

$my_links_categories = get_terms( 'link_category', 'orderby=count&hide_empty=0' );

//列出自定义分类中的所有术语,不带链接:
$terms = get_terms( 'my_taxonomy' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
	echo '<ul>';
	foreach ( $terms as $term ) {
		echo '<li>' . $term->name . '</li>';
	}
	echo '</ul>';
}

5、列出所有术语,并带有术语存档的链接

$args = array( 'hide_empty=0' );

$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
	$count = count( $terms );
	$i = 0;
	$term_list = '<p class="my_term-archive">';
	foreach ( $terms as $term ) {
		$i++;
		$term_list .= '<a href="' . esc_url( get_term_link( $term ) ) . '" alt="' . esc_attr( sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) ) . '">' . $term->name . '</a>';
		if ( $count != $i ) {
			$term_list .= ' · ';
		}
		else {
			$term_list .= '</p>';
		}
	}
	echo $term_list;
}

6、获取与特定meta_key匹配的分类法的分类

$args = array(
    'hide_empty' => false, 
	'meta_query' => array(
      array(
         'key'       => 'project_status',
         'value'     => 'New',
         'compare'   => '='
      )
 ));
$projects = get_terms( 'Projects', $args );

再来一个:

$terms = get_terms( array(
    'taxonomy' => 'tax_slug',
    'hide_empty' => false,
    'meta_query' => array(
        [
            'key' => 'meta_key_slug_1',
            'value' => 'desired value to look for'
        ]
    ),
    'meta_key' => 'meta_key_slug_2',
    'orderby' => 'meta_key_slug_2'

) );

7、获取父分类的所有子分类

function get_child_taxonomies( $taxonomy_name, $termId, $args = array() ) {
	$defaults = array(
		'taxonomy'   => $taxonomy_name,
		'orderby'    => 'name',
		'order'      => 'ASC',
		'hide_empty' => true,
		'child_of'   => $termId,
	);

	$args       = wp_parse_args( $args, $defaults );
	$taxonomies = get_terms( $args );

	if ( empty( $taxonomies ) || is_wp_error( $taxonomies ) ) {
		return false;
	}	
	return $taxonomies;

}

如果“get_terms”由于某种奇怪的原因不起作用,并且自定义分类未显示已注册,请尝试使用“WP_Term_Query”

$term_query = new WP_Term_Query( array( 
    'taxonomy' => 'regions', // <-- Custom Taxonomy name..
    'orderby'                => 'name',
    'order'                  => 'ASC',
    'child_of'               => 0,
    'parent'                 => 0,
    'fields'                 => 'all',
    'hide_empty'             => false,
) );

// Show Array info
echo '<pre>';
print_r( $term_query->terms );
echo '</pre>';


//Render html
if ( ! empty( $term_query->terms ) ) {
    foreach ( $term_query->terms as $term ) {
        echo wp_kses_post( $term->name ) . ", ";
        echo esc_html( $term->term_id ) . ", ";
        echo esc_html( $term->slug ) . ", ";
        echo "<br>";
    }
} else {
    echo 'No term found.';
}
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容