1 year ago
#234490

Magikstah
How to check if current logged user is author of post
Im new to Timber/Twig. I want to display "edit button" near posts that already logged in user is author of (wordpress).
Im using loop to display posts, if I use {{user.id}
and {{post.author.id}}
i get current user ID and ID of posts author.
How can I use conditional to display button near posts that current user is author of?
I tried to use {% if user.id == post.author.id %}
but It doesn't work. I tried to replace ==
with ===
.
Do you have any idea?
<?php
use Timber\PostQuery;
use Timber\Timber;
$context = Timber::context();
$context['posts'] = Timber::get_posts(['post_type'=>'product']);
Timber::render('templates/pages/my-yachts-product.twig', $context);
<div class="product__buttons flex items-center justify-end flex-wrap">
{% include "templates/atoms/button.twig" with {title: __('Details', 'empressia'), url: product.link, class: 'btn--medium'} %}
{% if user.id == post.author.id %}
<a href="http://localhost/ansebsail/edit-yacht/?post={{ product.id }}" class="btn btn--medium"> Edytuj </a>
{% endif %}
</div>
EDIT - Function that I use to loop "products" which are modified posts
function FilterPostsAjax()
{
$response = [
'html' => '',
'description' => '',
'total_posts' => 0
];
$posts_query = $_POST;
$posts_query = $this->formatQuery($posts_query);
$wpQuery = new PostQuery($posts_query);
$response['query'] = $posts_query;
$response['total_posts'] = $wpQuery->found_posts;
$response['pagination'] = Timber::compile('components/pagination/pagination.twig', ['pagination' => (array)$wpQuery->pagination(5)]);
$posts = $wpQuery->get_posts();
$context = Timber::context();
$countPosts = count($posts);
if ($countPosts) {
$context['layout'] = 'wide';
foreach ($posts as $post) {
$context['product'] = $post;
if (isset($posts_query['is_favourites']) && $posts_query['is_favourites']) {
$context['is_favourites'] = true;
}
$response['html'] .= Timber::compile("templates/molecules/product.twig", $context);
}
}
echo json_encode($response);
die();
}
php
wordpress
twig
timber
0 Answers
Your Answer