get_taxonomies()获取所有的分类法信息

目录

描述

Get a list of registered taxonomy objects.

get_taxonomies() 描述

用法

 <?php get_taxonomies$args$output$operator ?> 

get_taxonomies() 用法

参数

$args

(array) (可选) An array of key -> value arguments to match against the taxonomies. Only taxonomies having attributes that match all arguments are returned.

默认值: None

  • name
  • object_type (array)
  • label
  • singular_label
  • show_ui
  • show_tagcloud
  • public
  • update_count_callback
  • rewrite
  • query_var
  • manage_cap
  • edit_cap
  • delete_cap
  • assign_cap
  • _builtin

$output

(string) (可选) The type of output to return, either taxonomy 'names' or 'objects'.

默认值: 'names'

$operator

(string) (可选) Operator (and/or) to use with multiple $args.

默认值: and

get_taxonomies() 参数

示例

Default Usage

The call to get_taxonomies returns the registered taxonomies.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $taxonomies = get_taxonomies(); ?>

Output a list all registered taxonomies

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php

$taxonomies = get_taxonomies(); 
foreach ( $taxonomies as $taxonomy ) {
    echo '<p>' . $taxonomy . '</p>';
}

?>

Output a list of all public custom taxonomies

This return all custom (not builtin) taxonomies.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php 
$args = array(
  'public'   => true,
  '_builtin' => false
  
); 
$output = 'names'; // or objects
$operator = 'and'; // 'and' or 'or'
$taxonomies = get_taxonomies( $args, $output, $operator ); 
if ( $taxonomies ) {
  foreach ( $taxonomies  as $taxonomy ) {
    echo '<p>' . $taxonomy . '</p>';
  }
}
?>

Output a named taxonomy

This example uses the 'object' output to retrieve and display the taxonomy called 'genre':

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php 
$args=array(
  'name' => 'genre'
);
$output = 'objects'; // or names
$taxonomies=get_taxonomies($args,$output); 
if  ($taxonomies) {
  foreach ($taxonomies  as $taxonomy ) {
    echo '<p>' . $taxonomy->name . '</p>';
  }
}  
?>

get_taxonomies() 示例

历史

  • 添加于 版本: 3.0.0

get_taxonomies() 历史

源文件

get_taxonomies() 函数的代码位于 wp-includes/taxonomy.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieves a list of registered taxonomy names or objects.
 *
 * @since 3.0.0
 *
 * @global array $wp_taxonomies The registered taxonomies.
 *
 * @param array  $args     Optional. An array of `key => value` arguments to match against the taxonomy objects.
 *                         Default empty array.
 * @param string $output   Optional. The type of output to return in the array. Accepts either taxonomy 'names'
 *                         or 'objects'. Default 'names'.
 * @param string $operator Optional. The logical operation to perform. Accepts 'and' or 'or'. 'or' means only
 *                         one element from the array needs to match; 'and' means all elements must match.
 *                         Default 'and'.
 * @return array A list of taxonomy names or objects.
 */
function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
	global $wp_taxonomies;

	$field = ('names' == $output) ? 'name' : false;

	return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
}

get_taxonomies() 源文件

相关