首页 > 解决方案 > How can I access {{comment.author.roles}}?

问题描述

I want to add a special style for admins who comment on a blog. I'm attempting to pull the roles for comment authors via {{ commment.author.roles }} per https://timber.github.io/docs/reference/timber-comment/#author, but it doesn't seem to return the proper roles, or even a normal \User object.

I came across this issue thread https://github.com/timber/timber/issues/1866 which seems to indicate this issue was fixed and merged, but it still doesn't seem to work for me.

I'm using WordPress v5.1.1 and Timber v1.9.4.

My single.php is pretty simple:

$context = Timber::get_context();
$post = Timber::query_post();
$context['post'] = $post

Timber::render( array( 'single-' . $post->ID . '.twig', 'single-' . $post->post_type . '.twig', 'single.twig' ), $context );

Then in single.twig calling my comment.twig partial:

<ol class="comments__list" id="commentlist">
    {% if post.comments %}
        {% for cmt in post.comments %}
            {% include "comment.twig" with {comment:cmt} %}
        {% endfor %}
    {% endif %}
</ol>

And in comment.twig attempting to use {{comment.author.roles|join(', ')}} returns NULL.

Also if I just dump the comment author ala {{ dump(comment.author) }}

I get a seemingly truncated version of a \User object:

object(Timber\User)#1635 (12) { ["object_type"]=> string(4) "user" ["_link"]=> NULL ["avatar_override"]=> NULL ["description"]=> NULL ["display_name"]=> NULL ["first_name"]=> NULL ["last_name"]=> NULL ["id"]=> NULL ["user_nicename"]=> NULL ["roles":protected]=> NULL ["ID"]=> NULL ["name"]=> string(10) "Josh Masen" }

Any input on what I might be doing wrong here would be very much appreciated. I'm still pretty new to Timber so I decided to post here instead of creating an issue because I feel like I'm probably missing something.

标签: wordpresstimber

解决方案


推荐阅读