“Function Reference/get post”的版本间的差异

来自WordPress中文文档
跳转至: 导航搜索
Return: NOTE: Was it pre-2.5?
(1个修订)
(没有差异)

2009年3月14日 (六) 09:23的版本

Description

Takes a post ID and returns the database record for that post. You can specify, by means of the tt$output/tt parameter, how you would like the results returned.

Usage

%%%?php get_post($post, $output); ? %%%

Example

To get the title for a post with ID 7:

%%%?php $my_id = 7; $post_id_7 = get_post($my_id); $title = $post_id_7-post_title; ? %%%

Alternatively, specify the tt$output/tt parameter:

%%%?php $my_id = 7; $post_id_7 = get_post($my_id, ARRAY_A); $title = $post_id_7['post_title']; ? %%%

%%%?php

    1. Correct: pass a dummy variable as post_id

$the_post = get_post( $dummy_id = 7 );

    1. Incorrect: literal integer as post_id

$the_post = get_post( 7 ); // Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference' ?%%%

Parameters

tt$post/tt
(integer) (required) The ID of the post you'd like to fetch. You must pass a variable containing an integer (e.g. tt$id/tt). A literal integer (e.g. tt7/tt) will cause a fatal error (Only variables can be passed for reference or Cannot pass parameter 1 by reference).
Default: Nonenoinclude

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$output/tt
(string) (optional) How you'd like the result. ulliOBJECT - returns an object/liliARRAY_A - Returns an associative array of field names to values/liliARRAY_N - returns a numeric array of field values/li/ul
Default: OBJECTnoinclude

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

The fields returned are:

ttID/tt 
(integer) The post ID
ttpost_author/tt 
(integer) The post author's ID
ttpost_date/tt 
(string) The datetime of the post (ttYYYY-MM-DD HH:MM:SS/tt)
ttpost_date_gmt/tt 
(string) The GMT datetime of the post (ttYYYY-MM-DD HH:MM:SS/tt)
ttpost_content/tt 
(string) The post's contents
ttpost_title/tt 
(string) The post's title
ttpost_category/tt 
(integer) The post category's ID. Note that this will always be 0 (zero) from wordpress 2.1 onwards. To determine a post's category or categories, use get_the_category().
ttpost_excerpt/tt 
(string) The post excerpt
ttpost_status/tt 
(string) The post status (ttpublish|pending|draft|private|static|object|attachment|inherit|future/tt)
ttcomment_status/tt 
(string) The comment status (ttopen|closed|registered_only/tt)
ttping_status/tt 
(string) The pingback/trackback status (ttopen|closed/tt)
ttpost_password/tt 
(string) The post password
ttpost_name/tt 
(string) The post's URL slug
ttto_ping/tt 
(string) URLs to be pinged
ttpinged/tt 
(string) URLs already pinged
ttpost_modified/tt 
(string) The last modified datetime of the post (ttYYYY-MM-DD HH:MM:SS/tt)
ttpost_modified_gmt/tt 
(string) The last modified GMT datetime of the post (ttYYYY-MM-DD HH:MM:SS/tt)
ttpost_content_filtered/tt 
(string)
ttpost_parent/tt 
(integer) The parent post's ID (for attachments, etc)
ttguid/tt 
(string) A link to the post. Note: One cannot rely upon the GUID to be the permalink (as it previously was in pre-2.5), Nor can you expect it to be a valid link to the post. It's mearly a unique identifier, which so happens to be a link to the post at present.
ttmenu_order/tt 
(integer)
ttpost_type/tt 
(string) (ttpost|page|attachment/tt)
ttpost_mime_type/tt 
(string) Mime Type (for attachments, etc)
ttcomment_count/tt 
(integer) Number of comments

References

get_post method not working NB: This topic has been closed to new replies.

includeonlydiv style=clear:both; background-color:#EEEEFF; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This article is marked as in need of editing. You can help Codex by editing it./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

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

Result

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

/noinclude