Timber Logo

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

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

Properties #

NameTypeDescription
$IDint
$idint
$comment_author_emailstring
$comment_contentstring
$comment_datestring
$comment_IDint
$user_idint
$post_idint
$comment_authorstring

Methods #

NameReturn TypeSummary/Returns
__toString()stringGets the content.
add_child()arrayAdds a child.

Returns: Comment children.
approved()boolIs the comment approved?
author()\Timber\UserGets the author.
avatar()bool or mixed or stringFetches the Gravatar.
can_edit()boolChecks whether the current user can edit the comment.
children()arrayGets the comment children.

Returns: Comments
content()stringGets the content.
date()stringThe date for the comment.
depth()intAt what depth is this comment?
edit_link()string or nullGets 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.
get_field()mixedGets a comment meta value.

Returns: The meta field value.
get_meta_field()mixedGets a comment meta value.

Returns: The meta field value.
is_child()boolChecks if the comment is a child.
reply_link()stringEnqueue the WP threaded comments JavaScript, and fetch the reply link for various comments.
time()stringWhat 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

NameTypeDescription
$sizeint or mixedSize of avatar.
$defaultstringDefault avatar URL.

Twig

<img src="{{comment.avatar(36,template_uri~"/img/dude.jpg")}}" alt="Image of {{comment.author.name}}" />

HTML

<img src="http://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.

NameTypeDescription
$child_comment\Timber\CommentComment child to add.

update_depth() #

Updates the comment depth.

update_depth( int $depth = '' )

NameTypeDescription
$depthintLevel 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

NameTypeDescription
$date_formatstringof 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

NameTypeDescription
$time_formatstringof 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.

NameTypeDescription
$field_namestringThe 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.

see Timber\Comment::meta()

get_field( string $field_name = null )

Returns: mixed The meta field value.

NameTypeDescription
$field_namestringThe field name for which you want to get the value.

Enqueue the WP threaded comments JavaScript, and fetch the reply link for various comments.

reply_link( string $reply_text = 'Reply' )

Returns: string

NameTypeDescription
$reply_textstringText 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 %}

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 %}