post

$post global variables represents the current post in the WordPress loop. It is set in the method the_post() of the the class WP_Query defined in [wordpress root folder]/wp-includes/classes.php. This method has a wrapper in form of a global function the_post() defined in [wordpress root folder]/wp-includes/functions.php. This global function is invoked as part of the WordPress Loop.

The theme authors need not use this global variable directly. WordPress provides functions to display the post data inside the loop.

  • the_ID()
  • the_title(): displays title
  • the_content(): displays the post content
  • the_excerpt(): displays the excerpt
  • the_attachment_link(): displays the attachment link
  • the_meta(): displays the post metadata which comprises the post custom fields

These functions display the values after applying filters. To access them for custom processing WordPress provides get_ equivalents of the functions mentioned above, which takes the post ID as the parameter, e.g., get_the_title($id) which will return title of the specified post.

To access the post custom fields following functions are provided:

  • get_post_custom_keys(): returns an array of keys of all the custom fields.
  • get_post_custom_values($key = ''): returns arrays of values of custom fields with specified key
  • get_post_meta($post_id, $key, $single = false): returns value of the specified custom field key for specified post_id

Back to full list of global variables.

Technorati tags: , , ,

One Response to “post”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: