当前浏览:首页 / WordPress函数 / wp_list_categories()

wp_list_categories( array|string $args = '' )

显示或检索类别的HTML列表

cat

category 分类more...

listmore...


参数

$args

(array|string) (可选) 可选参数数组,有关可接受参数的信息,请参阅get_categories()get_terms()WP_Term_Query::__construct()

  • 'current_category'
    (int|int[]) 类别的ID或ID数组,应获得 'current-cat' css类,默认为0
  • 'depth'
    (int) 类别深度,用于制表符缩进,默认为0
  • 'echo'
    (bool|int) 是输出还是返回生成的结果,接受0,1,或其布尔等价值。默认为1
  • 'exclude'
    (int[]|string) 要排除的term(分类项) ID数组或逗号/空格分隔的字符串。如果 $hierarchical 为 true,$exclude 排除的 term 的后代也将被排除。见 $exclude_tree,见 get_terms()
  • 'exclude_tree'
    (int[]|string) 要排除的term ID的数组或逗号/空格分隔的字符串,以及它们的后代。见 get_terms()
  • 'feed'
    (string) feed 链接使用的文本,默认:'Feed for all posts filed under [cat name]'
  • 'feed_image'
    (string) 用于 feed 链接的图片的URL
  • 'feed_type'
    (string) feed 类型,用来建立feed链接。见 get_term_feed_link()。默认为空字符串(默认feed)
  • 'hide_title_if_empty'
    (bool) 如果列表中没有term,是否隐藏 $title_li 元素。默认为false(标题将始终显示)
  • 'separator'
    (string) 链接之间的分隔符。默认为 <br />
  • 'show_count'
    (bool|int) 是否包含文章数量。接受0,1,或其布尔等价值。默认为0
  • 'show_option_all'
    (string) 显示“所有类别”的文本
  • 'show_option_none'
    (string) 为"没有类别"选项显示的文本。默认为"No categories"
  • 'style'
    (string) 用于显示类别列表的方式。如果是'list',类别将被输出为无序列表。如果留空或其他值,类别将以 <br> 标签分隔输出。默认为 "list"。
  • 'taxonomy'
    (string) 要检索的分类法(taxonomy)名称,默认值 'category'
  • 'title_li'
    (string) 列表 <li> 元素标题的文本,传递空值用于禁用,默认:'Categories'
  • 'use_desc_for_title'
    (bool|int) 是否使用类别描述作为标题属性。接受0、1或其布尔等价值。默认为1
  • 'walker'
    (Walker) 用来构建输出的 Walker 对象,默认为空,导致使用 Walker_Category 实例

默认值: ''


返回

(void|string|false) 如果'echo'参数为true,则无返回,如果'echo'为false,则返回类别的HTML列表。如果分类(taxonomy)不存在,则为False。


说明

如果需要无格式化结果的函数,请尝试get_categories()

用法

默认情况下,wp_list_categories()显示:

  • 没有链接到所有类别
  • 按类别名称升序对类别列表进行排序
  • 以无序列表的方式显示
  • 不显示文章数量
  • 只显示有文章的类别
  • 将标题属性设置为类别描述
  • 不限于任何类别的子类
  • 没有使用 feed 或 feed 图片
  • 不排除任何类别,包含所有类别
  • 显示具有CSS类 'current-cat' 的当前类别
  • 以分层缩进的方式显示类别
  • 将类别显示为列表上的标题
  • 没有强制 SQL LIMIT (‘number’ => 0 未显示在上面)
  • 输出类别
  • 深度无限制
  • 所有类别
  • 列表使用一个新的 Walker_Category 类的Walker对象进行渲染。


源码

查看源码 官方文档


更新日志

版本描述
4.4.0current_category参数被修改为可选地接受一个数组值
2.1.0开始引入

使用示例

  • 示例1

    显示分配给文章的类别

    显示分配给按父子类别关系排序的文章的类别(或其他分类中的term)。类似于函数get_the_category_list(),它按名称对类别进行排序。此示例必须在循环内使用。

    $taxonomy = 'category';
    
    // Get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    
    // Separator between links.
    $separator = ', ';
    
    if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
    
    	$term_ids = implode( ',' , $post_terms );
    
    	$terms = wp_list_categories( array(
    		'title_li' => '',
    		'style'    => 'none',
    		'echo'     => false,
    		'taxonomy' => $taxonomy,
    		'include'  => $term_ids
    	) );
    
    	$terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
    
    	// Display post categories.
    	echo  $terms;
    }
    
  • 示例2

    包括或排除类别

    要按字母顺序对类别进行排序并仅包括ID为16、3、9和5的类别,可以编写以下代码:

    <ul>
    	<?php wp_list_categories( array(
    		'orderby' => 'name',
    		'include' => array( 3, 5, 9, 16 )
    	) ); ?> 
    </ul>
    

    以下示例显示按名称排序的类别链接,显示每个类别的文章数,并从列表中排除ID为10的类别。

    <ul>
    	<?php wp_list_categories( array(
    		'orderby'    => 'name',
    		'show_count' => true,
    		'exclude'    => array( 10 )
    	) ); ?> 
    </ul>
    
  • 示例3

    显示或隐藏列表标题

    title_li 参数设置或隐藏由wp_list_categories生成的类别列表的标题。它默认为“(__(‘Categories’)”,即它将单词“Categories”显示为列表的标题。如果参数设置为null或空值,则不显示标题。以下示例代码排除ID为4和7的类别,并隐藏列表标题:

    <ul>
    	<?php wp_list_categories( array(
    		'exclude'  => array( 4,7 ),
    		'title_li' => ''
    	) ); ?>
    </ul>
    

    在以下示例中,列表中仅包括ID为9、5和23的类别,标题文本已更改为单词“Poetry”,标题样式为:

    <ul>
    	<?php wp_list_categories( array(
    		'include'  => array( 5, 9, 23 ),
    		'title_li' => '<h2>' . __( 'Poetry', 'textdomain' ) . '</h2>'
    	) ); ?> 
    </ul>
    
  • 示例4

    注意,您可以使用为get_terms()函数和WP_Term_Query::__construct实例的所有参数。因此,例如,可以添加参数meta_keymeta_value,它们也可以发挥作用。

  • 示例5

    从类别计数中删除括号

    当show_count=1时,每个类别计数都用括号括起来。为了在不修改核心WordPress文件的情况下删除括号,请使用以下代码。

    $variable = wp_list_categories( array(
    	'echo' => false,
    	'show_count' => true,
    	'title_li' => '<h2>' . __( 'Categories', 'textdomain' ) . '</h2>'
    ) );
    
    $variable = preg_replace( '~((d+))(?=s*+<)~', '$1', $variable );
    echo $variable;
    
  • 示例6

    在自定义分类中显示分类项(term)

    在3.0版本中,添加了taxonomy参数,以启用wp_list_categories()函数来列出自定义分类。本例列出了分类 genre 中的分类项:

    // List terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
    
    $taxonomy     = 'genre';
    $orderby      = 'name'; 
    $show_count   = false;
    $pad_counts   = false;
    $hierarchical = true;
    $title        = '';
    
    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );
    ?>
    
    <ul>
    	<?php wp_list_categories( $args ); ?>
    </ul>
    
  • 示例7

    所有类别由逗号分隔。

    strip_tags( get_the_category_list( ', ', ', ' ) )
  • 示例8

    仅显示类别的子类

    以下示例代码仅为ID为8的类别的子类别生成按ID排序的类别链接,它显示每个类别的文章数量,并从生成链接的标题属性中隐藏类别描述。注意:如果父类别中没有文章,则不会显示父类别。

    <ul>
    	<?php wp_list_categories( array(
    		'orderby'            => 'id',
    		'show_count'         => true,
    		'use_desc_for_title' => false,
    		'child_of'           => 8
    	) ); ?>
    </ul>
    
  • 示例9

    显示带有RSS Feed链接的类别

    以下示例生成按名称排序的类别链接,显示每个类别的文章数,并显示每个类别的RSS feed链接。

    <ul>
    	<?php wp_list_categories( array(
    		'orderby'    => 'name',
    		'show_count' => true,
    		'feed'       => 'RSS'
    	) ); ?>
    </ul>
    

    要用feed图标替换rss链接,您可以编写:

    <ul>
    	<?php wp_list_categories( array(
    		'orderby'    => 'name',
    		'show_count' => true,
    		'feed_image' => '/images/rss.gif'
    	) ); ?>
    </ul>
    
  • 示例10

    类别列表的标记和样式

    默认情况下,wp_list_categories() 在标题为“categories”的单个列表项(li)中生成嵌套无序列表(ul)。

    通过将title_li参数设置为空字符串,可以删除最外层的项和列表。您需要自己将输出包装在有序列表(ol)或无序列表中(参见上面的示例)。如果根本不希望以列表输出,请将style参数设置为none。

    您可以使用以下CSS选择器设置输出样式:

     li.categories { ... }  /* outermost list item */
     li.cat-item { ... }
     li.cat-item-7 { ... }  /* category ID #7, etc */
     li.current-cat { ... }
     li.current-cat-parent { ... }
     ul.children { ... }
    
  • 示例11

    参数列表中缺少“include”。