Post
A better post object. Ditch "The Loop".
And in case you missed it, every template gets passed a $Post
and $Posts
variable.
Read more about this in the Default Data section.
Usage
Single
<?php /** @var \WPDev\Models\Post $Post */ ?>
<h1>
<a href="<?= $Post->url(); ?>">
<?= $Post->title(); ?>
</a>
</h1>
<?= $Post->content(); ?>
Loop
<?php /** @var \WPDev\Models\Post[] $Posts */ ?>
<?php foreach ($Posts as $post) : ?>
<div>
<?= $post->featuredImage('medium'); ?>
<h2><?= $post->title(); ?></h2>
<div><?= $post->excerpt(); ?></div>
<a href="<?= $post->url(); ?>">Read More</a>
</div>
<?php endforeach; ?>
API Reference
Public Methods
Method | Summary |
__construct |
Constructor. For a more fluid syntax use Post::create() .
|
create
static |
Alternative to constructor. For more fluid syntax. |
acfField |
Uses ACF's get_field() to fetch a field value.
|
acfFields |
Uses ACF's get_fields() to get all custom field values in an associative array.
|
ancestors |
Retrieves the parent posts in direct parent to highest level ancestor order. Each post will be an instance of \WPDev\Models\Post .
|
createdDate | The date the post was created. |
featuredImage |
Get the featured image. Returns an instance of \WPDev\Models\Image .
|
featuredImageId | Get the featured image id. |
field |
Gets a field value using get_post_meta() .
|
hasFeaturedImage | Whether the post has a featured image or not. |
hasTerm | Checks whether the post has the term. If no arg passed checks if it has any term. |
id | Post ID |
title | Post title |
url | Post url (aka permalink). |
content |
Same as the_content() except we don't echo.
|
excerpt | Post excerpt |
modifiedDate | The last modified date. |
parent |
Gets the parent post if any. If so will return a \WPDev\Models\Post object.
|
parentId | The parent ID if there is a parent. Else 0. |
postType | The post type |
status | The current post status |
taxonomies | The taxonomy names associated with the post. |
terms | Terms for all or a specific associated taxonomy. |
wpPost |
The original WP_Post object
|
-
__construct
__construct( int | \WP_Post | null
$post
)SummaryConstructor. For a more fluid syntax usePost::create()
.ParameterspostOptional. Post ID or post object. Defaults to global $post. -
create
create( int | \WP_Post | null
$post
)SummaryAlternative to constructor. For more fluid syntax.ParameterspostOptional. Post ID or post object. Defaults to `$GLOBALS['post']`.Returns$this
-
acfField
acfField( string
$selector
[, bool$format
] )SummaryUses ACF'sget_field()
to fetch a field value.ParametersselectorThe field keyformatShould ACF format the value for youReturnsmixed | null
-
acfFields
acfFields( [ bool
$format
] )SummaryUses ACF'sget_fields()
to get all custom field values in an associative array.ParametersformatWhether ACF should format the values.Returnsarray
Associate array with all custom field values
-
ancestors
ancestors( )
SummaryRetrieves the parent posts in direct parent to highest level ancestor order. Each post will be an instance of\WPDev\Models\Post
.Returns\WPDev\Models\Post[]
Array of \WPDev\Models\Post objects
-
createdDate
createdDate( [ string
$date_format
] )SummaryThe date the post was created.Parametersdate_formatA date format string. Defaults to `get_option('date_format')` date format set in the WP backend.Returnsbool | string
The formatted date. False on failure.
-
featuredImage
featuredImage( [ string
$size
] )SummaryGet the featured image. Returns an instance of\WPDev\Models\Image
.ParameterssizeThe image size to use.Returns\WPDev\Models\Image
-
featuredImageId
featuredImageId( )
SummaryGet the featured image id.Returnsint
-
field
field( string
$key
[, bool$single_value
] )SummaryGets a field value usingget_post_meta()
.ParameterskeyThe field key (aka meta key).single_valueWhether WP should return the value or the value wrapped in an array.Returnsmixed
The field value if it exists. Else an empty string if $single_value = true or an empty array if $single_value = false.
-
hasFeaturedImage
hasFeaturedImage( )
SummaryWhether the post has a featured image or not.Returnsbool
-
hasTerm
hasTerm( [ string | int | array
$term
, string$taxonomy_name
] )SummaryChecks whether the post has the term. If no arg passed checks if it has any term.ParameterstermThe term name/term_id/slug or array of them to check for.taxonomy_nameTaxonomy nameReturnsbool
True if the current post has any of the given tags (or any tag, if no tag specified).
-
id
id( )
SummaryPost IDReturnsint
-
title
title( )
SummaryPost titleReturnsstring
-
url
url( )
SummaryPost url (aka permalink).Returnsbool | string
-
content
content( null
$more_link_text
[, bool$strip_teaser
] )SummarySame asthe_content()
except we don't echo.Parametersmore_link_textstrip_teaserReturnsstring
-
excerpt
excerpt( )
SummaryPost excerptReturnsstring
-
modifiedDate
modifiedDate( [ string
$date_format
] )SummaryThe last modified date.Parametersdate_formatReturnsbool | string
-
parent
parent( )
SummaryGets the parent post if any. If so will return a\WPDev\Models\Post
object.Returnsbool | \WPDev\Models\Post
-
parentId
parentId( )
SummaryThe parent ID if there is a parent. Else 0.Returnsint
-
postType
postType( )
SummaryThe post typeReturnsstring
-
status
status( )
SummaryThe current post statusReturnsbool | string
-
taxonomies
taxonomies( )
SummaryThe taxonomy names associated with the post.Returnsarray
Taxonomy names
-
terms
terms( null
$taxonomy_name
)SummaryTerms for all or a specific associated taxonomy.Parameterstaxonomy_nameReturnsarray | mixed
-
wpPost
wpPost( )
SummaryThe originalWP_Post
objectReturns\WP_Post | null