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 | |
$id | int | |
$comment_author_email | string | |
$comment_content | string | |
$comment_date | string | |
$comment_ID | int | |
$user_id | int | |
$post_id | int | |
$comment_author | string |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__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. |
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. |
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. |
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 #
__toString() #
Gets the content.
Returns: string
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" />
content() #
Gets the content.
Returns: string
children() #
Gets the comment children.
Returns: array
Comments
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. |
update_depth() #
Updates the comment depth.
update_depth( int $depth = '' )
Name | Type | Description |
---|---|---|
$depth | int | Level of depth. |
depth() #
At what depth is this comment?
Returns: int
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 %}
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>
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>
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
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. |
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. |
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 %}
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 %}