Function Reference/get post types

来自WordPress中文文档
跳转至: 导航搜索

Description

Returns the registered post types as found in $wp_post_types.

Usage

%%% <?php get_post_types( $args, $output, $operator ) ?> %%%

Parameters

tt$args/tt
(array) (optional) An array of key value arguments to match against the post types.
Default: array()noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude 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.
tt$output/tt
(string) (optional) The type of output to return, either 'names' or 'objects'.
Default: 'names'noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$operator/tt
(string) (optional) Operator (and/or) to use with multiple $args.
Default: 'and'noinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return Values

array 
A list of post names or objects.

Examples

Default Usage

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

<?php $post_types = get_post_types(); ?>

Output a list all registered post types

<?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.

<?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':

<?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>';
}

?>

Notes

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)

Change Log

  • 3.0 :
    • Added the operator parameter.
  • Since: 2.9

Source File

get_post_types() is located in onlyincludecodewp-includes/post.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

Post Types: register_post_type, add_post_type_support, remove_post_type_support, post_type_supports, set_post_type, is_post_type_hierarchical, post_type_exists, get_post_type, get_post_type_object, get_post_types, get_post_type_capabilities, get_post_type_labels

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude