WordPress: Function Reference/get post:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
(新页面: ==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. ==Us...)
 
无编辑摘要
第1行: 第1行:
==Description==
==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.
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.
得到文章ID并且返回那篇文章的数据库记录。你可以通过<tt>$output</tt>参数规定,你希望结果以什么样的方式返回。


==Usage==
==Usage==
==用法==
%%%<?php get_post($post, $output); ?> %%%


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


==Example==
==Example==
==例子==


To get the title for a post with ID 7:
To get the title for a post with ID 7:
得到一篇文章ID为7的文章标题:
%%%<?php
$my_id = 7;
$post_id_7 = get_post($my_id);
$title = $post_id_7->post_title;
?> %%%


%%%<?php
%%%<?php
第18行: 第36行:


Alternatively, specify the <tt>$output</tt> parameter:
Alternatively, specify the <tt>$output</tt> parameter:
此外,规定<tt>$output</tt>参数:
%%%<?php
$my_id = 7;
$post_id_7 = get_post($my_id, ARRAY_A);
$title = $post_id_7['post_title'];
?> %%%


%%%<?php
%%%<?php
第24行: 第52行:
$title = $post_id_7['post_title'];
$title = $post_id_7['post_title'];
?> %%%
?> %%%


%%%<?php
%%%<?php
第29行: 第58行:
$the_post = & get_post( $dummy_id = 7 );
$the_post = & get_post( $dummy_id = 7 );
%%%<?php
## 正确的: 将一个名义变数作为post_id传递
$the_post = & get_post( $dummy_id = 7 );
## Incorrect: literal integer as post_id
## Incorrect: literal integer as post_id
##不正确的:字面意思上的整数作为post_id
$the_post = & get_post( 7 );
$the_post = & get_post( 7 );
// Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
// Fatal error: 'Only variables can be passed for reference' or 'Cannot pass parameter 1 by reference'
?>%%%
$the_post = & get_post( 7 );
// 致命错误: '只有变数可以作为 reference传递' 或者 '不能够通过reference传递参数1'
?>%%%
?>%%%


==Parameters==
==Parameters==
==参数==


{{Parameter|$post|integer|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. <tt>7</tt>) will cause a fatal error (''Only variables can be passed for reference'' or ''Cannot pass parameter 1 by reference'').}}
{{Parameter|$post|integer|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. <tt>7</tt>) will cause a fatal error (''Only variables can be passed for reference'' or ''Cannot pass parameter 1 by reference'').}}
{{参数|$post|整数|你想要获取的文章的ID。'''你必须传递一个包含有整数的变数'''(例如<tt>$id</tt>)。一个整数(例如<tt>7</tt>)会导致一个致命的错误(''只有变数可以作为reference传递'' 或者''不能够通过reference传递参数1)。}}
{{Parameter|$output|string|How you'd like the result. <ul><li>''OBJECT'' - returns an object</li><li>''ARRAY_A'' - Returns an associative array of field names to values</li><li>''ARRAY_N'' - returns a numeric array of field values</li></ul>|optional|OBJECT}}
{{Parameter|$output|string|How you'd like the result. <ul><li>''OBJECT'' - returns an object</li><li>''ARRAY_A'' - Returns an associative array of field names to values</li><li>''ARRAY_N'' - returns a numeric array of field values</li></ul>|optional|OBJECT}}
{{参数|$output|string|你希望结果是怎样显示的。 <ul><li>''OBJECT'' – 返回object</li><li>''ARRAY_A'' – 返回参数值field名称的相关数组</li><li>''ARRAY_N'' – 返回field值</li></ul>|optional|OBJECT数字数组}}


==Return==
==Return==
==返回==


The fields returned are:
The fields returned are:
 
返回的field是:
; <tt>ID</tt> : (''integer'') The post ID
; <tt>ID</tt> : (''integer'') The post ID
; <tt>post_author</tt> : (''integer'') The post author's ID
; <tt>post_author</tt> : (''integer'') The post author's ID
第67行: 第122行:
; <tt>post_mime_type</tt> : (''string'') Mime Type (for attachments, etc)
; <tt>post_mime_type</tt> : (''string'') Mime Type (for attachments, etc)
; <tt>comment_count</tt> : (''integer'') Number of comments
; <tt>comment_count</tt> : (''integer'') Number of comments
; <tt>ID</tt> : (''integer'') 文章 ID
; <tt>post_author</tt> : (''integer'') 文章作者的ID
; <tt>post_date</tt> : (''string'') 文章发表的日期(<tt>YYYY-MM-DD HH:MM:SS</tt>)
; <tt>post_date_gmt</tt> : (''string'') 文章发表的格林尼治时间(<tt>YYYY-MM-DD HH:MM:SS</tt>)
; <tt>post_content</tt> : (''string'') 文章的内容
; <tt>post_title</tt> : (''string'') 文章的标题
; <tt>post_category</tt> : (''integer'')文章的类别ID。从WordPress2.1开始,这个ID通过是0(零)。要决定一篇文章的类别,请使用get_the_category()。
; <tt>post_excerpt</tt> : (''string'') 文章摘录
; <tt>post_status</tt> : (''string'') 文章级别(<tt>publish|draft|private|static|object|attachment|inherit|future</tt>)
; <tt>comment_status</tt> : (''string'') 评论级别(<tt>open|closed|registered_only</tt>)
; <tt>ping_status</tt> : (''string'') pingback/trackback 级别(<tt>open|closed</tt>)
; <tt>post_password</tt> : (''string'') 文章密码
; <tt>post_name</tt> : (''string'') 文章的URL slug
; <tt>to_ping</tt> : (''string'') 将要ping 的URLs
; <tt>pinged</tt> : (''string'') 已经ping的URLs
; <tt>post_modified</tt> : (''string'') 文章最后更改的时间(<tt>YYYY-MM-DD HH:MM:SS</tt>)
; <tt>post_modified_gmt</tt> : (''string'') 文章最后更改的格林尼治时间 (<tt>YYYY-MM-DD HH:MM:SS</tt>)
; <tt>post_content_filtered</tt> : (''string'')
; <tt>post_parent</tt> : (''integer'') 母文章ID (对于附属,等等)
; <tt>guid</tt> : (''string'')文章的permalink
; <tt>menu_order</tt> : (''integer'')
; <tt>post_type</tt> : (''string'') (<tt>post|page|attachment</tt>)
; <tt>post_mime_type</tt> : (''string'') Mime Type (对于附属,等等)
; <tt>comment_count</tt> : (''integer'')评论的数目


== References ==
== References ==
==参考 ==


[http://wordpress.org/support/topic/54424 get_post method not working] NB: "This topic has been closed to new replies."
[http://wordpress.org/support/topic/54424 get_post method not working] NB: "This topic has been closed to new replies."
{{Copyedit}}
[http://wordpress.org/support/topic/54424 get_post method not working] NB: "这个话题不接受新的回复。"


{{Copyedit}}
{{Copyedit}}

2008年7月18日 (五) 16:42的版本

Description

描述

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

得到文章ID并且返回那篇文章的数据库记录。你可以通过$output参数规定,你希望结果以什么样的方式返回。

Usage

用法

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

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

Example

例子

To get the title for a post with ID 7:

得到一篇文章ID为7的文章标题:

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

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

Alternatively, specify the $output parameter:

此外,规定$output参数:

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


%%%<?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 );


%%%<?php

    1. 正确的: 将一个名义变数作为post_id传递

$the_post = & get_post( $dummy_id = 7 );


    1. Incorrect: literal integer as post_id
    1. 不正确的:字面意思上的整数作为post_id

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

$the_post = & get_post( 7 ); // 致命错误: '只有变数可以作为 reference传递' 或者 '不能够通过reference传递参数1' ?>%%%

Parameters

参数

模板:参数


模板:参数


Return

返回

The fields returned are: 返回的field是:

ID
(integer) The post ID
post_author
(integer) The post author's ID
post_date
(string) The datetime of the post (YYYY-MM-DD HH:MM:SS)
post_date_gmt
(string) The GMT datetime of the post (YYYY-MM-DD HH:MM:SS)
post_content
(string) The post's contents
post_title
(string) The post's title
post_category
(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().
post_excerpt
(string) The post excerpt
post_status
(string) The post status (publish|draft|private|static|object|attachment|inherit|future)
comment_status
(string) The comment status (open|closed|registered_only)
ping_status
(string) The pingback/trackback status (open|closed)
post_password
(string) The post password
post_name
(string) The post's URL slug
to_ping
(string) URLs to be pinged
pinged
(string) URLs already pinged
post_modified
(string) The last modified datetime of the post (YYYY-MM-DD HH:MM:SS)
post_modified_gmt
(string) The last modified GMT datetime of the post (YYYY-MM-DD HH:MM:SS)
post_content_filtered
(string)
post_parent
(integer) The parent post's ID (for attachments, etc)
guid
(string) The post's permalink
menu_order
(integer)
post_type
(string) (post|page|attachment)
post_mime_type
(string) Mime Type (for attachments, etc)
comment_count
(integer) Number of comments


ID
(integer) 文章 ID
post_author
(integer) 文章作者的ID
post_date
(string) 文章发表的日期(YYYY-MM-DD HH:MM:SS)
post_date_gmt
(string) 文章发表的格林尼治时间(YYYY-MM-DD HH:MM:SS)
post_content
(string) 文章的内容
post_title
(string) 文章的标题


post_category
(integer)文章的类别ID。从WordPress2.1开始,这个ID通过是0(零)。要决定一篇文章的类别,请使用get_the_category()。
post_excerpt
(string) 文章摘录
post_status
(string) 文章级别(publish|draft|private|static|object|attachment|inherit|future)
comment_status
(string) 评论级别(open|closed|registered_only)
ping_status
(string) pingback/trackback 级别(open|closed)
post_password
(string) 文章密码
post_name
(string) 文章的URL slug
to_ping
(string) 将要ping 的URLs
pinged
(string) 已经ping的URLs
post_modified
(string) 文章最后更改的时间(YYYY-MM-DD HH:MM:SS)
post_modified_gmt
(string) 文章最后更改的格林尼治时间 (YYYY-MM-DD HH:MM:SS)
post_content_filtered
(string)
post_parent
(integer) 母文章ID (对于附属,等等)
guid
(string)文章的permalink
menu_order
(integer)
post_type
(string) (post|page|attachment)
post_mime_type
(string) Mime Type (对于附属,等等)
comment_count
(integer)评论的数目




References

参考

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

This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.

get_post method not working NB: "这个话题不接受新的回复。"

This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.