The Site Object
To ensure you can use the function, make sure you followed the instructions on the initializing page. It’s literally one step, you can’t mess it up.
You’re free to rename the function if it clashes with any other names, but this documentation assumes you’ve instantiated the
better-wordpress class into a variable named
$site and it is referenced by a global function named
site() function in any of your theme’s php files will return the
ofc/Site object, a class with a collection of convenient methods to interface with various parts of WordPress.
One convenient feature of better-wordpress is the ability to define the structure of a returned post object on certain methods.
In implimentation all this means is creating a string array of
WP_Post members you want returned. For example, if I only want to get the
$post_author of a post, my fields would look like this:
$fields = ['post_title','post_author'] // Get these fields from post with id 4 $post = site()->getPost(4,$fields)
The result of getPost is the following:
[ "post_title" => "My Blog Post", "post_author" => "Escher" ]
Check out this
getPost example to see some of these fields in action.
id– Shortcut to return the
title– Shortcut to return the
name– Shortcut to return the
get_permalink($id)to fetch the url of the post.
permalink– Same as
get_the_post_thumbnail_url($id)to get the post thumbnail.
content– Retrieves the content of the post.
excerpt– Retrieves the post excerpt, if enabled.
meta.<key>– Get the value of a specific post meta field.
acf.<key>– Get the value of a specific ACF field for this post.
categories.<member1>,<member2>,...– Get specific members of the
WP_Termfor each category this post is a part of.
taxonomy.<taxonomy>.<member1>,<member2>,...– Get specific members of the
WP_Termfor each term within the given taxonomy this post is a part of. See below for members:
id– Alias for
link– The result of
title– Alias for