The Post Dynamic Shortcode is a powerful tool designed to fetch and display specific data related to WordPress posts. It allows for fine-tuned data retrieval by specifying post fields, using key arguments for additional filtering, and even accessing post meta that isn't publicly exposed. Ideal for developers and content creators, it enhances the flexibility and control of content within WordPress pages or posts.
Syntax
The syntax for the Post Dynamic Shortcode is as follows:
{post:field}
field
represents the specific post data you want to retrieve, such as the post title, author, or custom meta.
Key Arguments
id
Specifies the ID of the post from which to retrieve data. If not provided, the current post is used.
{post:title@id=42}
all
A boolean key argument that, when set, returns all metadata associated with the specified post field.
{post:field@all}
taxonomy
When retrieving terms, this argument specifies which taxonomy's terms to retrieve (default is category). Note: only applicable with the 'terms' field.
{post:terms@taxonomy=books}
Examples
Retrieve the Title of the Current Post
{post:title}
This shortcode will fetch and display the title of the current post.Retrieve the Title of a Specific Post
{post:title@id=42}
This shortcode will fetch and display the title of the post with the ID 42.
Access a Custom Field Value
{post:custom_field_name}
This retrieves the value of custom_field_name from the current post.
Get the Excerpt of the Current Post
{post:excerpt}
This example will return the excerpt for the current post being viewed.
List Terms Associated with a Post
{for:single-term
{post:terms@taxonomy=category id=10}
[<li>{term:name @ID={get:single-term}}]
}
Here, the shortcode lists terms from the 'category' taxonomy associated with the post that has the ID 10.{post:terms@taxonomy=category id=10}
returns an array so we cycled the terms with the shortcode {for}.
Special Privileges
Accessing certain post meta, particularly those starting with an underscore (which are typically private), requires the shortcode to be used within a Power Shortcode that has elevated privileges.
Additionally, if the post is not publicly visible (e.g., not published), elevated privileges are also required to retrieve its data.