Timber\Comment
The Timber\Comment
class is used to view the output of comments. 99% of the time this will be in the context of the comments on a post. However you can also fetch a comment directly using its comment ID.
PHP
$comment = Timber::get_comment( $comment_id );
$context = [
'comment_of_the_day' => $comment
];
Timber::render('index.twig', $context);
Twig
<p class="comment">{{comment_of_the_day.content}}</p>
<p class="comment-attribution">- {{comment.author.name}}</p>
HTML
<p class="comment">But, O Sarah! If the dead can come back to this earth and flit unseen around those they loved, I shall always be near you; in the garish day and in the darkest night -- amidst your happiest scenes and gloomiest hours - always, always; and if there be a soft breeze upon your cheek, it shall be my breath; or the cool air fans your throbbing temple, it shall be my spirit passing by.</p>
<p class="comment-attribution">- Sullivan Ballou</p>
Overview #
This class extends Timber\CoreEntity
This class implements Stringable
Properties #
Name | Type | Description |
---|---|---|
$ID | int | |
$comment_ID | int | |
$comment_author | string | |
$comment_author_email | string | |
$comment_content | string | |
$comment_date | string | |
$id | int | |
$post_id | int | |
$user_id | int |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__call() | mixed | Magic method dispatcher for meta fields, for convenience in Twig views. Returns: The value of the meta field named $field if truthy, false otherwise. |
__toString() | string | Gets the content. |
add_child() | array | Adds a child. Returns: Comment children. |
approved() | bool | Is the comment approved? |
author() | \Timber\User | Gets the author. |
avatar() | bool or mixed or string | Fetches the Gravatar. |
can_edit() | bool | Checks whether the current user can edit the comment. |
children() | array | Gets the comment children. Returns: Comments |
content() | string | Gets the content. |
convert() | Finds any WP_Post objects and converts them to Timber\Posts | |
date() | string | The date for the comment. |
depth() | int | At what depth is this comment? |
edit_link() | string or null | Gets the edit link for a comment if the current user has the correct rights. Returns: The edit URL of a comment in the WordPress admin or null if the current user can’t edit the comment. |
fetch_meta() | mixed | Gets an object meta value. Returns: The custom field value or an array of custom field values. Null if no value could be found. |
mixed | Gets a comment meta value. Returns: The meta field value. | |
mixed | Gets a comment meta value. Returns: The meta field value. | |
is_child() | bool | Checks if the comment is a child. |
meta() | mixed | Gets an object meta value. Returns: The custom field value or an array of custom field values. Null if no value could be found. |
raw_meta() | null or mixed | Gets an object meta value directly from the database. Returns: The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found. |
reply_link() | string | Enqueue the WP threaded comments JavaScript, and fetch the reply link for various comments. |
time() | string | What time was the comment posted? |
update_depth() | Updates the comment depth. |
Class Methods #
__call() #
Magic method dispatcher for meta fields, for convenience in Twig views.
Called when explicitly invoking non-existent methods on a Core object. This method is not meant to be called directly.
link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call
__call( string $field, array $arguments )
Returns: mixed
The value of the meta field named $field
if truthy, false
otherwise.
This method is inherited from \Timber\CoreEntity
.
Name | Type | Description |
---|---|---|
$field | string | The name of the method being called. |
$arguments | array | Enumerated array containing the parameters passed to the function. Not used. |
PHP
$post = Timber\Post::get( get_the_ID() );
update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );
Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );
Twig
{# Since this method does not exist explicitly on the Post class,
it will dynamically dispatch the magic __call() method with an argument
of "favorite_zep4_track" #}
<span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>
__toString() #
Gets the content.
Returns: string
add_child() #
Adds a child.
add_child( \Timber\Comment $child_comment )
Returns: array
Comment children.
Name | Type | Description |
---|---|---|
$child_comment | \Timber\Comment | Comment child to add. |
approved() #
Is the comment approved?
Returns: bool
Twig
{% if comment.approved %}
Your comment is good
{% else %}
Do you kiss your mother with that mouth?
{% endif %}
author() #
Gets the author.
Returns: \Timber\User
Twig
<h3>Comments by...</h3>
<ol>
{% for comment in post.comments %}
<li>{{comment.author.name}}, who is a {{comment.author.roles[0]}}</li>
{% endfor %}
</ol>
HTML
<h3>Comments by...</h3>
<ol>
<li>Jared Novack, who is a contributor</li>
<li>Katie Ricci, who is a subscriber</li>
<li>Rebecca Pearl, who is a author</li>
</ol>
avatar() #
Fetches the Gravatar.
avatar( int|mixed $size = 92, string $default = '' )
Returns: bool|mixed|string
Name | Type | Description |
---|---|---|
$size | int or mixed | Size of avatar. |
$default | string | Default avatar URL. |
Twig
<img src="{{comment.avatar(36,template_uri~"/img/dude.jpg")}}" alt="Image of {{comment.author.name}}" />
HTML
<img src="https://gravatar.com/i/sfsfsdfasdfsfa.jpg" alt="Image of Katherine Rich" />
can_edit() #
Checks whether the current user can edit the comment.
Returns: bool
Twig
{% if comment.can_edit %}
<a href="{{ comment.edit_link }}">Edit</a>
{% endif %}
children() #
Gets the comment children.
Returns: array
Comments
content() #
Gets the content.
Returns: string
convert() #
Finds any WP_Post objects and converts them to Timber\Posts
convert( array|\Timber\CoreEntity $data )
This method is inherited from \Timber\CoreEntity
.
Name | Type | Description |
---|---|---|
$data | array or \Timber\CoreEntity |
date() #
The date for the comment.
date( string $date_format = '' )
Returns: string
Name | Type | Description |
---|---|---|
$date_format | string | of desired PHP date format (eg "M j, Y"). |
Twig
{% for comment in post.comments %}
<article class="comment">
<p class="date">Posted on {{ comment.date }}:</p>
<p class="comment">{{ comment.content }}</p>
</article>
{% endfor %}
HTML
<article class="comment">
<p class="date">Posted on September 28, 2015:</p>
<p class="comment">Happy Birthday!</p>
</article>
depth() #
At what depth is this comment?
Returns: int
edit_link() #
Gets the edit link for a comment if the current user has the correct rights.
since 2.0.0
Returns: string|null
The edit URL of a comment in the WordPress admin or null if the current user can’t edit the comment.
Twig
{% if comment.can_edit %}
<a href="{{ comment.edit_link }}">Edit</a>
{% endif %}
fetch_meta() #
Gets an object meta value.
Returns a meta value or all meta values for all custom fields of an object saved in the object meta database table.
Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.
fetch_meta( string $field_name = '', array $args = [], bool $apply_filters = true )
Returns: mixed
The custom field value or an array of custom field values. Null if no value could be found.
This method is inherited from \Timber\CoreEntity
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string. |
$args | array | An array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array. |
$apply_filters | bool | Whether to apply filtering of meta values. You can also use the raw_meta() method as a shortcut to apply this argument. Default true. |
get_field() #
Gets a comment meta value.
DEPRECATED since 2.0.0, use {{ comment.meta('field_name') }}
instead.
get_field( string $field_name = null )
Returns: mixed
The meta field value.
Name | Type | Description |
---|---|---|
$field_name | string | The field name for which you want to get the value. |
get_meta_field() #
Gets a comment meta value.
DEPRECATED since 2.0.0, use {{ comment.meta('field_name') }}
instead.
get_meta_field( string $field_name )
Returns: mixed
The meta field value.
Name | Type | Description |
---|---|---|
$field_name | string | The field name for which you want to get the value. |
is_child() #
Checks if the comment is a child.
Returns: bool
meta() #
Gets an object meta value.
Returns a meta value or all meta values for all custom fields of an object saved in the meta database table.
Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.
meta( string $field_name = '', array $args = [] )
Returns: mixed
The custom field value or an array of custom field values. Null if no value could be found.
This method is inherited from \Timber\CoreEntity
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string. |
$args | array | An array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array. |
raw_meta() #
Gets an object meta value directly from the database.
Returns a raw meta value or all raw meta values saved in the meta database table. In comparison to meta()
, this function will return raw values that are not filtered by third- party plugins.
Fetching raw values for all custom fields will not have a big performance impact, because WordPress gets all meta values, when the first meta value is accessed.
since 2.0.0
raw_meta( string $field_name = '' )
Returns: null|mixed
The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.
This method is inherited from \Timber\CoreEntity
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string. |
reply_link() #
Enqueue the WP threaded comments JavaScript, and fetch the reply link for various comments.
reply_link( string $reply_text = 'Reply' )
Returns: string
Name | Type | Description |
---|---|---|
$reply_text | string | Text of the reply link. |
time() #
What time was the comment posted?
time( string $time_format = '' )
Returns: string
Name | Type | Description |
---|---|---|
$time_format | string | of desired PHP time format (eg "H:i:s"). |
Twig
{% for comment in post.comments %}
<article class="comment">
<p class="date">Posted on {{ comment.date }} at {{comment.time}}:</p>
<p class="comment">{{ comment.content }}</p>
</article>
{% endfor %}
HTML
<article class="comment">
<p class="date">Posted on September 28, 2015 at 12:45 am:</p>
<p class="comment">Happy Birthday!</p>
</article>
update_depth() #
Updates the comment depth.
update_depth( int $depth = '' )
Name | Type | Description |
---|---|---|
$depth | int | Level of depth. |