vi:Tham Khảo Các Hàm/get post
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.
<?php get_post($post, $output); ?>
To get the title for a post with ID 7:
$my_id = 7;
$post_id_7 = get_post($my_id);
$title = $post_id_7->post_title;
Alternatively, specify the $output parameter:
$my_id = 7;
$post_id_7 = get_post($my_id, ARRAY_A);
$title = $post_id_7['post_title'];
## Correct: pass a dummy variable as post_id
$the_post = & get_post( $dummy_id = 7 );
## 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'
- (integer) (required) The ID of the post you'd like to fetch. You must pass a variable containing an integer (e.g. $id). A literal integer (e.g. 7) will cause a fatal error (Only variables can be passed for reference or Cannot pass parameter 1 by reference).
- Default: None
- (string) (optional) How you'd like the result.
- OBJECT - returns an object
- ARRAY_A - Returns an associative array of field names to values
- ARRAY_N - returns a numeric array of field values
- Default: OBJECT
The fields returned are:
- (integer) The post ID
- (integer) The post author's ID
- (string) The datetime of the post (YYYY-MM-DD HH:MM:SS)
- (string) The GMT datetime of the post (YYYY-MM-DD HH:MM:SS)
- (string) The post's contents
- (string) The post's title
- (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().
- (string) The post excerpt
- (string) The post status (publish|pending|draft|private|static|object|attachment|inherit|future)
- (string) The comment status (open|closed|registered_only)
- (string) The pingback/trackback status (open|closed)
- (string) The post password
- (string) The post's URL slug
- (string) URLs to be pinged
- (string) URLs already pinged
- (string) The last modified datetime of the post (YYYY-MM-DD HH:MM:SS)
- (string) The last modified GMT datetime of the post (YYYY-MM-DD HH:MM:SS)
- (integer) The parent post's ID (for attachments, etc)
- (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.
- (string) (post|page|attachment)
- (string) Mime Type (for attachments, etc)
- (integer) Number of comments
get_post method not working NB: "This topic has been closed to new replies."