WordPress function: wp_list_categories (category list) example and principle

原型:wp_list_categories( string|array $args = ” )

1. By default, this function generates an unordered list, you should use <ul></ul> to wrap the generated content.
2. The use of wp_list_categories() is similar to that of list_cats() and wp_list_cats(), but the latter two have been deprecated.
3. If you want to use this result as a variable, you can set "echo" => 0, and then operate according to the variable.
4. You can also use get_categories() to get categorical variables.
5. The parameter of this function accepts a string or an array, see the example below for details.
6. The following example test environment Wordpress4.8

 

<?php
	$args = array(
			"title_li"=>'<h2>' . __( '金三国', 'textdomain' ) . '</h2>',
			"show_option_all"=>'Home',
			"orderby"=>'ID',
			"style"=>'list',
			"show_count"=>1,
			"hide_empty"=>0,
			"use_desc_for_title"=>1,
			"child_of"=>0,
			"exclude"=>1,
			"hierarchical"=>1,
			"echo"=>1,
			"current_category"=>1,
			"hide_title_if_empty"=>1,
			"taxonomy"=>'category'
		);

	wp_list_categories($args);
?>

 Don't be frightened by these parameters, in fact, it is very simple, you only need to understand the general meaning of English, let's take a look at the example.

 

Example 1. Sort categories in alphabetical order, including only categories with category IDs 2, 4, 7, and 9. You can use the following code:

<?php
wp_list_categories('orderby=name&include=2,4,7,9');
?>

 

Example 2. Arrange by name, display the number of articles, and exclude the category with ID 7:

<?php
wp_list_categories('orderby=name&show_count=1&exclude=7');
?>

 

 

 Example 3. Display the category name and the number of articles to which the category belongs

<?php
wp_list_categories('show_count=1&title_li=category');
?>

 

Example 4. Display the sub-categories under the category, including the number of articles, and display the empty sub-categories at the same time

<?php
wp_list_categories('child_of=5&title_li=&show_count=1&hide_empty=0');
?>

 

Example 5. You can specify the title and style of the category

<?php
wp_list_categories('title_li=<h2>' . __( '金三国', 'textdomain' ) . '</h2>');
?>

 

Example 6. If you do not plan to use the classification in the form of an unordered list, you can refer to the form of hyperlinks and line breaks.

<?php
wp_list_categories('style=0');
?>

 

Example 7. Sort by category name, display the number of articles, and display the RSS subscription link of each category

<?php
wp_list_categories('orderby=ID&show_count=1&feed=RSS'); //It is rarely used now
?>

 

Example 8. In the hyperlinks displayed in categories, use the title attribute, which is beneficial to SEO

<?php
wp_list_categories('use_desc_for_title=1');
?>

 

Then let's take a general look at its parameter explanation, don't worry, you only need to know its existence and simple use of this function, because it is not used very much.

 

Parameter usage parameter value
show_option_all Specifies a hyperlink to the home page of the site The name of the home page (string)
order ascending or descending order ASC ascending (default),

 

DESC descending order

style The style of the category list display list output li,

 

none use <br > tags to separate list items

show_count Whether to display the number of articles 1,0
hide_empty Whether to hide categories without articles 1,0
use_desc_for_title Whether to show the title attribute on the link 1,0
child_of Only show subcategories of the category identified by this parameter For 8 Show subcategories of category with ID 8

 

0 to display all subcategories of the category

exclude Exclude one or more categories. Use commas to separate the IDs of each category, and the parameter include must be empty. 4,7
exclude_tree Exclude classification trees. Use commas to separate the IDs of each category, and the parameter include must be empty.  
include Show only specific categories, use commas to separate the IDs of each category 3,5,9,16
hierarchical Whether to display categories by hierarchy 1,0
title_li Sets the title of the list, defaults to "Categories". Pass empty string to disable
echo Display the result or save it in a variable 1,0
depth Shows the depth of the classification (ie how many layers of subclasses are shown) 0 – 所有分类和子分类(默认)
-1 – 所有类别显示在平面(不缩进)的形式(覆盖hierarchical)。
1 – 只显示顶级分类
n – 任意大于1的值,根据n的具体数字来显示对应等级的分类。
current_category 默认为0,在当前分类下加样式值,便于美化 为0则根据页面动图添加当前样式值,填指定分类id,则仅在指定分类中添加样式值
pad_counts 父分类的文章数是否包括子分类的链接或文章数。 1,0
taxonomy 给定分类法 默认category

随着Wordpress版本的更新,此函数的使用频率越来越低,本wordpress教程的意义在于,你只需了解以上例子功能、使用的环境,即可,比如,你碰到需要调阅分类的时候,能想起这个函数就可以了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326713253&siteId=291194637