wp_list_categories () function uses the method | wordpress function

  wp_list_categories () function is the classification set out in the form of a link, click on the link classification, you can visit the category page. We sometimes call category links in the page number, then this function you can spend. Note: wp_list_categories () and list_cats () and wp_list_cats ()] Similar, but behind two deprecated. If you do not want to format the output classification, use get_categories ()

<?php $args = array(
	'show_option_all'    => '',
	'orderby'            => 'name',
	'order'              => 'ASC',
	'style'              => 'list',
	'show_count'         => 0,
	'hide_empty'         => 1,
	'use_desc_for_title' => 1,
	'child_of'           => 0,
	'feed'               => '',
	'feed_type'          => '',
	'feed_image'         => '',
	'exclude'            => '',
	'exclude_tree'       => '',
	'include'            => '',
	'hierarchical'       => 1,
	'title_li'           => __( 'Categories' ),
	'show_option_none'   => __('No categories'),
	'number'             => null,
	'echo'               => 1,
	'depth'              => 0,
	'current_category'   => 0,
	'pad_counts'         => 0,
	'taxonomy'           => 'category',
	'walker'             => null
); ?>
<?php wp_list_categories( $args ); ?>

  

The default output effects Usage:

  • No classification connected
  • Classification list in ascending order according to the classification name
  • Are displayed in the style of unordered list
  • It does not show the number of articles
  • Show only articles of classification
  • Set the title attribute to describe classification
  • Unlimited sub-categories
  • Feed Feed image is not displayed and
  • Do not rule out any classification, and include all categories
  • Add CSS class 'current-cat' to the current classification
  • Show Category List indented in a hierarchical manner
  • At the top of the list shows "classification (Categories)" as the title
  • SQL is no limit ( 'number' => 0 is not shown above)
  • Display (output) Classification
  • Without limiting the depth displayed
  • all categories
  • Walker_Category using a new class object to display the list walker

parameter

show_option_all 

(String) non-null value will result if the style is set to display a list of links to all categories. The default is not to display links to all categories.

orderby 

(String) to the category name, category or ID number and other articles for the arrangement basis. Valid values:

  • ID
  • name - default
  • slug
  • count
  • term_group

order 

Sort order (string) sorted (ascending or descending). The default is ascending order. Valid values:

  • ASC - Ascending (default)
  • DESC - Descending

style

Style (string) category list displayed. The classification list displayed (using <br> tag-delimited list of items). The default setting is a list (unordered list). Valid values:

  • list - Default
  • none

show_count 

(Boolean) shows the number of articles. The number of articles is not displayed by default. Valid values:

  • 1 (True)
  • 0 (False) - Default

hide_empty

(Boolean) hide no classified article. Hide article no default. Valid values:

  • 1 (True) - Default
  • 0 (False)

use_desc_for_title 

(Boolean) whether the classification as described in the title attribute category links, default is true. Valid values:

  • 1 (True) - Default
  • 0 (False)

child_of 

(Integer) shows only sub-categories of classification for this parameter is determined. There is no default value for this parameter.

feed 

(Character string) displayed text for each classification feed RSS-2 Feed linked to the set. Not displayed by default feed text and links.

feed_type 

(String)

feed_image 

(String) set a picture to RSS-2 Feed link classification. This parameter overrides the feed parameter above. There is no default value for this parameter.

exclude 

(String) exclude one or more classification. ID using commas to separate each of the categories. Parameters include must be empty.

exclude_tree 

(String) exclude classification tree. ID using commas to separate each of the categories. Parameters include must be empty. If hierarchical parameter is true, use the exclude parameter substitution exclude_tree.

include 

(String) display to a specific classification, using commas to separate each category ID.

hierarchical

(Boolean) displays a hierarchical classification of sub-classification. The default is true (display them in the following sub-categories classification). Valid values:

  • 1 (True) - Default
  • 0 (False)

title_li 

(String) set the title of the list. The default is "Categories". If this parameter exists, but is empty, the list will not be displayed.

number

Classification number (integer) of the setting display. Define the limits of SQL. The default is no limit.

echo

(Boolean) displays the results or saved in a variable. The default is True. Valid values:

  • 1 (True) - Default
  • 0 (False)

depth 

(Integer) display depth classification (i.e., how many layers of the display sub-classification). The default value is 0 (show all categories and subcategories). Valid values:

  • 0 - All categories and subcategories (default)
  • -1-- all categories is displayed in the form of a plane (not indented) a (covered hierarchical).
  • 1-- show only top-level classification
  • n - depending on the number n of the corresponding level of classification is displayed.

current_category 

(Integer) followed by the classification ID number, this classification ID name added CSS control the current-cat, the user-defined current-cat style, can be used to highlight.

pad_counts

(Boolean) or links to calculate the number of articles, including sub-classification. If show_counts and hierarchical to true, this parameter is automatically set to true. Valid values:

  • 1 (true)
  • 0 (false) - Default

taxonomy 

(String) to return taxonomy. This parameter is added in 3.0. Valid values:

  • category - Default
  • taxonomy - or any other registered taxonomy

walker 

(Target) to generate a list Walker classes. Valid values:

object - an instance of a class that extends either Walker_Category or Walker

 

wp_list_categories () function uses the method of Example

Sorted alphabetically, only the classification includes a category ID 16,3,9 and 5, you can use the following code:

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

  The arrangement of names, display the number of articles, and the classification ID 10 is excluded:

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

  

Show or hide the list of title
title_li parameters can be set or hide the title wp_list_categories function to generate the list. The default title is "classification (Categories)". If set to null, it will not display the title. Exclude classification ID 4 and 7, as well as hide the title:

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

  Show only the classification ID 23 and 5,9, and the title list to Poetry, but using <h2> tag comprising:

<ul>
<?php wp_list_categories('include=5,9,23&title_li=<h2>' . __('Poetry') . '</h2>' ); ?> 
</ul>

  

Subcategories display only a specified category
display ID as sub-categories of classification 8, according to the sort ID, display the number of articles, and classified as described in connection title attribute. Note: If the parent does not classify the article, will not display the parent classification.

<ul> 
<?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=8'); ?>
</ul>

  

Removing brackets count
when show_count = 1, behind each classification number of articles are displayed, while the use of parentheses contain. If you want to remove the brackets, you can use the following code

<?php
$variable = wp_list_categories('echo=0&show_count=1&title_li=<h2>Categories</h2>');
$variable = preg_replace('~\((\d+)\)(?=\s*+<)~', '$1', $variable);
echo $variable;
?>

  

Show Category RSS Feed connection and
sorted according to category names, shows the number of articles, links and display RSS feeds each category:

<ul>
<?php wp_list_categories('orderby=name&show_count=1&feed=RSS'); ?>
</ul>

  Feed use replacement text icons, can use the following code:

<ul>
<?php wp_list_categories('orderby=name&show_count=1&feed_image=/images/rss.gif'); ?>
</ul>

  

Display custom taxonomy project
in version 3.0 added a taxonomy parameter to wp_list_categories (). So that you can set the parameters to be displayed by taxonomy is a classification of the project under which classification. For example, to display the taxonomy of genre classification list:

<?php 
//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   = 0;      // 1 for yes, 0 for no
$pad_counts   = 0;      // 1 for yes, 0 for no
$hierarchical = 1;      // 1 for yes, 0 for no
$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>

  

Show articles corresponding classification
according to parent - child relationships to sort classification of the article. Similar get_the_category_list () function (classified according to the name of the sort). The following examples must be used in a loop:

<?php
// get the category IDs assigned to post
$categories = wp_get_post_categories( $post->ID, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
 
if ( $categories ) {
 
	$cat_ids = implode( ',' , $categories );
	$cats = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $cat_ids );
	$cats = rtrim( trim( str_replace( '<br />',  $separator, $cats ) ), $separator );
 
	// display post categories
	echo  $cats;
}
?>

  

Tag and styling sorted list
by default, wp_list_categories () is generated unordered list (UL), using <li> tag to include each category, and the title list is "Categories".

You can be null to hide the title by setting title_li. You can customize the package ordered list or unordered list. If you do not need to list output classification, you can style parameter is set to none.

You can style the output according to the following CSS selectors:

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 { ... }

  wp_list_categories () function definition file wp-includes / category-template.php

 

Guess you like

Origin www.cnblogs.com/ytkah/p/11855776.html