PHP Class Timber\Post

Inheritance: extends Core, implements Timber\CoreInterface
Mostra file Open project: timber/timber Class Usage Examples

Public Properties

Property Type Description
$ID string the numeric WordPress id of a post, capitalized to match WP usage
$ImageClass string the name of the class to handle images by default
$PostClass string the name of the class to handle posts by default
$TermClass string the name of the class to handle terms by default
$class string stores the CSS classes for the post (ex: "post post-type-book post-123")
$id string the numeric WordPress id of a post
$object_type string what does this class represent in WordPress terms?
$post_author integer the numeric ID of the a post's author corresponding to the wp_user dtable
$post_content string the raw text of a WP post as stored in the database
$post_date string the raw date string as stored in the WP database, ex: 2014-07-05 18:01:39
$post_excerpt string the raw text of a manual post excerpt as stored in the database
$post_parent integer the numeric ID of a post's parent post
$post_status string the status of a post ("draft", "publish", etc.)
$post_title string the raw text of a post's title as stored in the database
$post_type string the name of the post type, this is the machine name (so "my_custom_post_type" as opposed to "My Custom Post Type")
$representation string what does this class represent in WordPress terms?
$slug string the URL-safe slug, this corresponds to the poorly-named "post_name" in the WP database, ex: "hello-world"

Protected Properties

Property Type Description
$__type Timber\PostType
$_content string stores the processed content internally
$_next array stores the results of the next Timber\Post in a set inside an array (in order to manage by-taxonomy)
$_permalink string the returned permalink from WP's get_permalink function
$_prev array stores the results of the previous Timber\Post in a set inside an array (in order to manage by-taxonomy)

Public Methods

Method Description
__construct ( mixed $pid = null ) If you send the constructor nothing it will try to figure out the current post id based on being inside The_Loop
__toString ( ) : string Outputs the title of the post if you do something like

{{post}}

author ( ) : User | null Return the author of a post
authors ( )
categories ( ) : array Get the categoires on a particular post
category ( ) : TimberTerm | null Returns a category attached to a post
children ( string | array $post_type = 'any', string | boolean $childPostClass = false ) : array Returns an array of children on the post as Timber\Posts (or other claass as you define).
comment_form ( array $args = [] ) : string Gets the comment form for use on a single article page
comments ( integer $count, string $order = 'wp', string $type = 'comment', string $status = 'approve', string $CommentClass = 'Timber\Comment' ) : boolean | array Gets the comments on a Timber\Post and returns them as an array of TimberComments (or whatever comment class you set).
content ( integer $page, $len ) : string Gets the actual content of a WP Post, as opposed to post_content this will run the hooks/filters attached to the_content. \This guy will return your posts content with WordPress filters run on it (like for shortcodes and wpautop).
convert ( array $data, string $class ) Finds any WP_Post objects and converts them to Timber\Posts
date ( string $date_format = '' ) : string Get the date to use in your template!
edit_link ( ) : boolean | string Returns the edit URL of a post if the user has access to it
format ( ) : mixed
get_author ( ) : User | null Gets a User object from the author of the post
get_categories ( ) : array Get the categories for a post
get_category ( ) : mixed
get_children ( string $post_type = 'any', boolean | string $childPostClass = false ) : array
get_comment_count ( ) : integer
get_comments ( integer $count, string $order = 'wp', string $type = 'comment', string $status = 'approve', string $CommentClass = 'Timber\Comment' ) : array | mixed Get the comments for a post
get_content ( integer $len, integer $page ) : string Displays the content of the post with filters, shortcodes and wpautop applied
get_date ( string $date_format = '' ) : string
get_edit_url ( ) : boolean | string Get the URL that will edit the current post/object
get_field ( string $field_name ) : mixed
get_format ( ) : mixed
get_image ( string $field ) : TimberImage
get_link ( ) : string get the permalink for a post object In your templates you should use link: Read my post
get_method_values ( ) : array
get_modified_author ( ) : User | null
get_modified_date ( string $date_format = '' ) : string
get_modified_time ( string $time_format = '' ) : string
get_next ( boolean $taxonomy = false ) : TimberPost | boolean Get the next post in WordPress's ordering
get_paged_content ( ) : string
get_pagination ( ) : array Get a data array of pagination so you can navigate to the previous/next for a paginated post
get_parent ( ) : boolean | TimberPost Get the parent post of the post
get_path ( ) : string Get the permalink for a post, but as a relative path For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post" this will return the relative version: "/2015/07/04/my-cool-post"
get_permalink ( ) : string
get_post_id_by_name ( string $post_name ) : integer get_post_id_by_name($post_name)
get_post_type ( ) : Timber\PostType Returns the post_type object with labels and other info
get_prev ( $in_same_term = false ) : TimberPost | boolean Get the next post in WordPress's ordering
get_preview ( integer $len = 50, boolean $force = false, string $readmore = 'Read More', boolean | string $strip = true, string $end = '…' ) : string get a preview of your post, if you have an excerpt it will use that, otherwise it will pull from the post_content.
get_tags ( ) : array Gets an array of tags for you to use
get_terms ( string | array $tax = '', boolean $merge = true, $TermClass = '' ) : array Get the terms associated with the post This goes across all taxonomies by default
get_thumbnail ( ) : Timber\Image | null
get_title ( ) : string Outputs the title with filters applied
has_field ( string $field_name ) : boolean
has_term ( string | integer $term_name_or_id, string $taxonomy = 'all' ) : boolean
import_custom ( integer $pid ) gets the post custom and attaches it to the current object
import_field ( string $field_name )
link ( ) : string get the permalink for a post object
meta ( string $field_name = null ) : mixed
modified_author ( ) : User | null Get the author (WordPress user) who last modified the post
modified_date ( string $date_format = '' ) : string
modified_time ( string $time_format = '' ) : string
name ( ) : string
next ( boolean $in_same_term = false ) : mixed
paged_content ( ) : string
pagination ( ) : array Get a data array of pagination so you can navigate to the previous/next for a paginated post
parent ( ) : boolean | Timber\Post Gets the parent (if one exists) from a post as a Timber\Post object (or whatever is set in Timber\Post::$PostClass)
password_required ( ) : boolean whether post requires password and correct password has been provided
path ( ) : string Gets the relative path of a WP Post, so while link() will return http://example.org/2015/07/my-cool-post this will return just /2015/07/my-cool-post
permalink ( ) : string
post_class ( string $class = '' ) : string Get the CSS classes for a post. For usage you should use {{post.class}} instead of {{post.post_class}}
prev ( boolean $in_same_term = false ) : mixed Get the previous post in a set
preview ( ) : PostPreview
tags ( ) : array Gets the tags on a post, uses WP's post_tag taxonomy
terms ( string | array $tax = '', boolean $merge = true, $TermClass = '' ) : array Get the terms associated with the post This goes across all taxonomies by default
thumbnail ( ) : TimberImage | null get the featured image as a TimberImage
time ( string $time_format = '' ) : string Get the time to use in your template
title ( ) : string Returns the processed title to be used in templates. This returns the title of the post after WP's filters have run. This is analogous to the_title() in standard WP template tags.
type ( ) : Timber\PostType Returns the post_type object with labels and other info
update ( string $field, mixed $value ) updates the post_meta of the current object with the given value

Protected Methods

Method Description
check_post_id ( integer $pid ) : integer helps you find the post id regardless of whether you send a string or whatever
determine_id ( $pid ) : integer tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through)
get_info ( integer $pid ) : null | object | WP_Pos\WP_Post Used internally by init, etc. to build TimberPost object
get_post_custom ( integer $pid ) : array Used internally to fetch the metadata fields (wp_postmeta table) and attach them to our TimberPost object
get_post_preview_id ( $query )
get_wp_link_page ( integer $i ) : string
init ( integer $pid = false ) Initializes a Post
maybe_show_password_form ( ) : string | void If the Password form is to be shown, show it!
prepare_post_info ( integer $pid ) : WP_Pos\WP_Post takes a mix of integer (post ID), string (post slug), or object to return a WordPress post object from WP's built-in get_post() function

Method Details

__construct() public method

If you send the constructor nothing it will try to figure out the current post id based on being inside The_Loop
public __construct ( mixed $pid = null )
$pid mixed

__toString() public method

Outputs the title of the post if you do something like

{{post}}

public __toString ( ) : string
return string

author() public method

Return the author of a post
public author ( ) : User | null
return User | null A User object if found, false if not

authors() public method

public authors ( )

categories() public method

Get the categoires on a particular post
public categories ( ) : array
return array of TimberTerms

category() public method

Returns a category attached to a post
public category ( ) : TimberTerm | null
return TimberTerm | null

check_post_id() protected method

helps you find the post id regardless of whether you send a string or whatever
protected check_post_id ( integer $pid ) : integer
$pid integer ;
return integer ID number of a post

children() public method

Returns an array of children on the post as Timber\Posts (or other claass as you define).
public children ( string | array $post_type = 'any', string | boolean $childPostClass = false ) : array
$post_type string | array _optional_ use to find children of a particular post type (attachment vs. page for example). You might want to restrict to certain types of children in case other stuff gets all mucked in there. You can use 'parent' to use the parent's post type or you can pass an array of post types.
$childPostClass string | boolean _optional_ a custom post class (ex: 'MyTimber\Post') to return the objects as. By default (false) it will use Timber\Post::$post_class value.
return array

comment_form() public method

Gets the comment form for use on a single article page
public comment_form ( array $args = [] ) : string
$args array this $args thing is a fucking mess, [fix at some point](http://codex.wordpress.org/Function_Reference/comment_form)
return string of HTML for the form

comments() public method

Gets the comments on a Timber\Post and returns them as an array of TimberComments (or whatever comment class you set).
public comments ( integer $count, string $order = 'wp', string $type = 'comment', string $status = 'approve', string $CommentClass = 'Timber\Comment' ) : boolean | array
$count integer Set the number of comments you want to get. `0` is analogous to "all"
$order string use ordering set in WordPress admin, or a different scheme
$type string For when other plugins use the comments table for their own special purposes, might be set to 'liveblog' or other depending on what's stored in yr comments table
$status string Could be 'pending', etc.
$CommentClass string What class to use when returning Comment objects. As you become a Timber pro, you might find yourself extending TimberComment for your site or app (obviously, totally optional)
return boolean | array

content() public method

Gets the actual content of a WP Post, as opposed to post_content this will run the hooks/filters attached to the_content. \This guy will return your posts content with WordPress filters run on it (like for shortcodes and wpautop).
public content ( integer $page, $len ) : string
$page integer
return string

convert() public method

Finds any WP_Post objects and converts them to Timber\Posts
public convert ( array $data, string $class )
$data array
$class string

date() public method

Get the date to use in your template!
public date ( string $date_format = '' ) : string
$date_format string
return string

determine_id() protected method

tries to figure out what post you want to get if not explictly defined (or if it is, allows it to be passed through)
protected determine_id ( $pid ) : integer
return integer the numberic id we should be using for this post object

format() public method

public format ( ) : mixed
return mixed

get_author() public method

Gets a User object from the author of the post
See also: Timber\Post::author
Deprecation: since 1.0
public get_author ( ) : User | null
return User | null

get_categories() public method

Get the categories for a post
See also: Timber\Post::categories
Deprecation: since 1.0
public get_categories ( ) : array
return array of TimberTerms

get_category() public method

See also: Timber\Post::category
Deprecation: since 1.0
public get_category ( ) : mixed
return mixed

get_children() public method

See also: Timber\Post::children
Deprecation: since 1.0
public get_children ( string $post_type = 'any', boolean | string $childPostClass = false ) : array
$post_type string
$childPostClass boolean | string
return array

get_comment_count() public method

public get_comment_count ( ) : integer
return integer the number of comments on a post

get_comments() public method

Get the comments for a post
See also: Timber\Post::comments
public get_comments ( integer $count, string $order = 'wp', string $type = 'comment', string $status = 'approve', string $CommentClass = 'Timber\Comment' ) : array | mixed
$count integer
$order string
$type string
$status string
$CommentClass string
return array | mixed

get_content() public method

Displays the content of the post with filters, shortcodes and wpautop applied
public get_content ( integer $len, integer $page ) : string
$len integer
$page integer
return string

get_date() public method

See also: Timber\Post::date
Deprecation: since 1.0
public get_date ( string $date_format = '' ) : string
$date_format string
return string

get_edit_url() public method

Get the URL that will edit the current post/object
See also: Timber\Post::edit_link
Deprecation: since 1.0
public get_edit_url ( ) : boolean | string
return boolean | string

get_field() public method

public get_field ( string $field_name ) : mixed
$field_name string
return mixed

get_format() public method

Deprecation: since 1.0
public get_format ( ) : mixed
return mixed

get_image() public method

public get_image ( string $field ) : TimberImage
$field string
return TimberImage

get_info() protected method

Used internally by init, etc. to build TimberPost object
protected get_info ( integer $pid ) : null | object | WP_Pos\WP_Post
$pid integer
return null | object | WP_Pos\WP_Post

get_method_values() public method

public get_method_values ( ) : array
return array

get_modified_author() public method

Deprecation: since 1.0
public get_modified_author ( ) : User | null
return User | null

get_modified_date() public method

See also: Timber\Post::modified_date
Deprecation: since 1.0
public get_modified_date ( string $date_format = '' ) : string
$date_format string
return string

get_modified_time() public method

public get_modified_time ( string $time_format = '' ) : string
$time_format string
return string

get_next() public method

Get the next post in WordPress's ordering
Deprecation: since 1.0
public get_next ( boolean $taxonomy = false ) : TimberPost | boolean
$taxonomy boolean
return TimberPost | boolean

get_paged_content() public method

public get_paged_content ( ) : string
return string

get_pagination() public method

Get a data array of pagination so you can navigate to the previous/next for a paginated post
Deprecation: since 1.0
public get_pagination ( ) : array
return array

get_parent() public method

Get the parent post of the post
Deprecation: since 1.0
public get_parent ( ) : boolean | TimberPost
return boolean | TimberPost

get_path() public method

Get the permalink for a post, but as a relative path For example, where {{post.link}} would return "http://example.org/2015/07/04/my-cool-post" this will return the relative version: "/2015/07/04/my-cool-post"
Deprecation: since 1.0
public get_path ( ) : string
return string

get_post_custom() protected method

Used internally to fetch the metadata fields (wp_postmeta table) and attach them to our TimberPost object
protected get_post_custom ( integer $pid ) : array
$pid integer
return array

get_post_id_by_name() public static method

get_post_id_by_name($post_name)
public static get_post_id_by_name ( string $post_name ) : integer
$post_name string
return integer

get_post_preview_id() protected method

protected get_post_preview_id ( $query )

get_post_type() public method

Returns the post_type object with labels and other info
Deprecation: since 1.0.4
public get_post_type ( ) : Timber\PostType
return Timber\PostType

get_prev() public method

Get the next post in WordPress's ordering
Deprecation: since 1.0
public get_prev ( $in_same_term = false ) : TimberPost | boolean
return TimberPost | boolean

get_preview() public method

If there's a tag it will use that to mark where to pull through.
public get_preview ( integer $len = 50, boolean $force = false, string $readmore = 'Read More', boolean | string $strip = true, string $end = '…' ) : string
$len integer The number of words that WP should use to make the tease. (Isn't this better than [this mess](http://wordpress.org/support/topic/changing-the-default-length-of-the_excerpt-1?replies=14)?). If you've set a post_excerpt on a post, we'll use that for the preview text; otherwise the first X words of the post_content
$force boolean What happens if your custom post excerpt is longer then the length requested? By default (`$force = false`) it will use the full `post_excerpt`. However, you can set this to true to *force* your excerpt to be of the desired length
$readmore string The text you want to use on the 'readmore' link
$strip boolean | string true for default, false for none, string for list of custom attributes
$end string The text to end the preview with (defaults to ...)
return string of the post preview

get_tags() public method

Gets an array of tags for you to use
Deprecation: since 1.0
public get_tags ( ) : array
return array

get_terms() public method

Get the terms associated with the post This goes across all taxonomies by default
Deprecation: since 1.0
public get_terms ( string | array $tax = '', boolean $merge = true, $TermClass = '' ) : array
$tax string | array What taxonom(y|ies) to pull from. Defaults to all registered taxonomies for the post type. You can use custom ones, or built-in WordPress taxonomies (category, tag). Timber plays nice and figures out that tag/tags/post_tag are all the same (and categories/category), for custom taxonomies you're on your own.
$merge boolean Should the resulting array be one big one (true)? Or should it be an array of sub-arrays for each taxonomy (false)?
return array

get_thumbnail() public method

See also: TimberPost::thumbnail
Deprecation: since 1.0
public get_thumbnail ( ) : Timber\Image | null
return Timber\Image | null

get_title() public method

Outputs the title with filters applied
Deprecation: since 1.0
public get_title ( ) : string
return string

has_field() public method

public has_field ( string $field_name ) : boolean
$field_name string
return boolean

has_term() public method

public has_term ( string | integer $term_name_or_id, string $taxonomy = 'all' ) : boolean
$term_name_or_id string | integer
$taxonomy string
return boolean

import_custom() public method

gets the post custom and attaches it to the current object
public import_custom ( integer $pid )
$pid integer a post ID number

import_field() public method

public import_field ( string $field_name )
$field_name string

init() protected method

Initializes a Post
protected init ( integer $pid = false )
$pid integer

maybe_show_password_form() protected method

If the Password form is to be shown, show it!
protected maybe_show_password_form ( ) : string | void
return string | void

meta() public method

public meta ( string $field_name = null ) : mixed
$field_name string
return mixed

modified_author() public method

Get the author (WordPress user) who last modified the post
public modified_author ( ) : User | null
return User | null A User object if found, false if not

modified_date() public method

public modified_date ( string $date_format = '' ) : string
$date_format string
return string

modified_time() public method

public modified_time ( string $time_format = '' ) : string
$time_format string
return string

name() public method

public name ( ) : string
return string

next() public method

public next ( boolean $in_same_term = false ) : mixed
$in_same_term boolean
return mixed

paged_content() public method

public paged_content ( ) : string
return string

pagination() public method

Get a data array of pagination so you can navigate to the previous/next for a paginated post
public pagination ( ) : array
return array

parent() public method

Gets the parent (if one exists) from a post as a Timber\Post object (or whatever is set in Timber\Post::$PostClass)
public parent ( ) : boolean | Timber\Post
return boolean | Timber\Post

password_required() public method

whether post requires password and correct password has been provided
public password_required ( ) : boolean
return boolean

path() public method

Gets the relative path of a WP Post, so while link() will return http://example.org/2015/07/my-cool-post this will return just /2015/07/my-cool-post
public path ( ) : string
return string

post_class() public method

Get the CSS classes for a post. For usage you should use {{post.class}} instead of {{post.post_class}}
See also: Timber\Post::$class
public post_class ( string $class = '' ) : string
$class string additional classes you want to add
return string a space-seperated list of classes

prepare_post_info() protected method

takes a mix of integer (post ID), string (post slug), or object to return a WordPress post object from WP's built-in get_post() function
protected prepare_post_info ( integer $pid ) : WP_Pos\WP_Post
$pid integer
return WP_Pos\WP_Post on success

prev() public method

Get the previous post in a set
public prev ( boolean $in_same_term = false ) : mixed
$in_same_term boolean
return mixed

preview() public method

public preview ( ) : PostPreview
return PostPreview

tags() public method

Gets the tags on a post, uses WP's post_tag taxonomy
public tags ( ) : array
return array

terms() public method

Get the terms associated with the post This goes across all taxonomies by default
public terms ( string | array $tax = '', boolean $merge = true, $TermClass = '' ) : array
$tax string | array What taxonom(y|ies) to pull from. Defaults to all registered taxonomies for the post type. You can use custom ones, or built-in WordPress taxonomies (category, tag). Timber plays nice and figures out that tag/tags/post_tag are all the same (and categories/category), for custom taxonomies you're on your own.
$merge boolean Should the resulting array be one big one (true)? Or should it be an array of sub-arrays for each taxonomy (false)?
return array

thumbnail() public method

get the featured image as a TimberImage
public thumbnail ( ) : TimberImage | null
return TimberImage | null of your thumbnail

time() public method

Get the time to use in your template
public time ( string $time_format = '' ) : string
$time_format string
return string

title() public method

Returns the processed title to be used in templates. This returns the title of the post after WP's filters have run. This is analogous to the_title() in standard WP template tags.
public title ( ) : string
return string

type() public method

Returns the post_type object with labels and other info
Since: 1.0.4
public type ( ) : Timber\PostType
return Timber\PostType

update() public method

updates the post_meta of the current object with the given value
public update ( string $field, mixed $value )
$field string
$value mixed

Property Details

$ID public_oe property

the numeric WordPress id of a post, capitalized to match WP usage
public string $ID
return string

$ImageClass public_oe property

the name of the class to handle images by default
public string $ImageClass
return string

$PostClass public_oe property

the name of the class to handle posts by default
public string $PostClass
return string

$TermClass public_oe property

the name of the class to handle terms by default
public string $TermClass
return string

$__type protected_oe property

protected PostType,Timber $__type
return Timber\PostType

$_content protected_oe property

stores the processed content internally
protected string $_content
return string

$_next protected_oe property

stores the results of the next Timber\Post in a set inside an array (in order to manage by-taxonomy)
protected array $_next
return array

$_prev protected_oe property

stores the results of the previous Timber\Post in a set inside an array (in order to manage by-taxonomy)
protected array $_prev
return array

$class public_oe property

stores the CSS classes for the post (ex: "post post-type-book post-123")
public string $class
return string

$id public_oe property

the numeric WordPress id of a post
public string $id
return string

$object_type public_oe property

what does this class represent in WordPress terms?
public string $object_type
return string

$post_author public_oe property

the numeric ID of the a post's author corresponding to the wp_user dtable
public int $post_author
return integer

$post_content public_oe property

the raw text of a WP post as stored in the database
public string $post_content
return string

$post_date public_oe property

the raw date string as stored in the WP database, ex: 2014-07-05 18:01:39
public string $post_date
return string

$post_excerpt public_oe property

the raw text of a manual post excerpt as stored in the database
public string $post_excerpt
return string

$post_parent public_oe property

the numeric ID of a post's parent post
public int $post_parent
return integer

$post_status public_oe property

the status of a post ("draft", "publish", etc.)
public string $post_status
return string

$post_title public_oe property

the raw text of a post's title as stored in the database
public string $post_title
return string

$post_type public_oe property

the name of the post type, this is the machine name (so "my_custom_post_type" as opposed to "My Custom Post Type")
public string $post_type
return string

$representation public_oe static_oe property

what does this class represent in WordPress terms?
public static string $representation
return string

$slug public_oe property

the URL-safe slug, this corresponds to the poorly-named "post_name" in the WP database, ex: "hello-world"
public string $slug
return string