get_post_types()获取所有的文章类型

目录

描述

Returns the registered post types as found in $wp_post_types.

get_post_types() 描述

用法

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

get_post_types() 用法

参数

$args

(array) (可选) An array of key value arguments to match against the post types.

默认值: array()

Some of these include:

  • public - Boolean. If true, only public post types will be returned.
  • publicly_queryable - Boolean
  • exclude_from_search - Boolean
  • show_ui - Boolean
  • capability_type
  • hierarchical
  • menu_position
  • menu_icon
  • permalink_epmask
  • rewrite
  • query_var
  • _builtin - Boolean. If true, will return WordPress default post types. Use false to return only custom post types.

$output

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

默认值: 'names'

$operator

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

默认值: 'and'

get_post_types() 参数

返回值

array 

A list of post names or objects.

get_post_types() 返回值

示例

Default Usage

The call to get post types returns the registered post types.

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

Output a list all registered post types

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

<?php

$post_types = get_post_types( '', 'names' ); 

foreach ( $post_types as $post_type ) {

   echo '<p>' . $post_type . '</p>';
}

?>

Output a list of all public custom post types

The built-in public post types are post, page, and attachment. By setting '_builtin' to false, we will exclude them and show only the custom public post types.

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

<?php

$args = array(
   'public'   => true,
   '_builtin' => false
);

$output = 'names'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'

$post_types = get_post_types( $args, $output, $operator ); 

foreach ( $post_types  as $post_type ) {

   echo '<p>' . $post_type . '</p>';
}

?>

Output a named post type

This example uses the 'object' output to retrieve the post type called 'property':

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

<?php

$args = array(
   'name' => 'property'
);

$output = 'objects'; // names or objects

$post_types = get_post_types( $args, $output );

foreach ( $post_types  as $post_type ) {

   echo '<p>' . $post_type->name . '</p>';
}

?>

get_post_types() 示例

注意

Be careful when retrieving "public" custom post-types that were registered using the register_post_type() function: the inputs to the register_post_type function are not intelligently processed, so if you verbosely set options for publicly_queriable, show_ui, show_in_nav_menus, and exclude_from_search, this is NOT considered equivalent to setting the public option and querying for public post-types will not yield results that were defined with the equivalent explicit arguments. See bug 18950.

WordPress builtin post types:

  • post - WordPress built-in post type
  • page - WordPress built-in post type
  • attachment - WordPress built-in post type
  • revision - WordPress built-in post type
  • nav_menu_item - WordPress built-in post type (Since 3.0)
  • custom post type - any custom post type (Since 3.0)

get_post_types() 注意

历史

  • 3.0 :
    • 添加 operator parameter.
  • 添加于 版本: 2.9

get_post_types() 历史

源文件

get_post_types() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Get a list of all registered post type objects.
 *
 * @since 2.9.0
 *
 * @global array $wp_post_types List of post types.
 *
 * @see register_post_type() for accepted arguments.
 *
 * @param array|string $args     Optional. An array of key => value arguments to match against
 *                               the post type objects. Default empty array.
 * @param string       $output   Optional. The type of output to return. Accepts post type 'names'
 *                               or 'objects'. Default 'names'.
 * @param string       $operator Optional. The logical operation to perform. 'or' means only one
 *                               element from the array needs to match; 'and' means all elements
 *                               must match. Accepts 'or' or 'and'. Default 'and'.
 * @return array A list of post type names or objects.
 */
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
	global $wp_post_types;

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

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

get_post_types() 源文件

相关