I’m trying to display all posts by a given author on the search results page using Timber. I’ve found that this works if I manually type it in:
/s?&author_name={username}
But I need to create these links dynamically in a loop, and unfortunately Timber’s User object doesn’t have access to a User’s username
. Going by ID also doesn’t work (/s?&author={author_id}
).
What’s the solution here?
2
Answers
I would suggest you make a function available in Twig which allows you to pass in the author id and return the author archive link via get_author_posts_url() or access the WP user class.
See documentation on how to achieve this:
https://timber.github.io/docs/guides/functions/#make-functions-available-in-twig
php
twig
If you need to access author archive via link, you can do it by
TimberPost
objectBut as I understood, your problem is to pass user login into twig templates. This way you can add to a global context all of your users.
search.php
search.twig