I have a list of posts with one term assigned for one post.
I have 5 terms. I generated a list of the term used. I would like to display only post with the same term in my archive page.
archive.php :
$templates = array( 'archive.twig', 'index.twig' );
$context = Timber::get_context();
$context['categories'] = Timber::get_terms('category', array('hide_empty' => true));
$context['title'] = 'Archive';
if ( is_day() ) {
$context['title'] = 'Archive: '.get_the_date( 'D M Y' );
} else if ( is_month() ) {
$context['title'] = 'Archive: '.get_the_date( 'M Y' );
} else if ( is_year() ) {
$context['title'] = 'Archive: '.get_the_date( 'Y' );
} else if ( is_tag() ) {
$context['title'] = single_tag_title( '', false );
} else if ( is_category() ) {
$context['title'] = single_cat_title( '', false );
array_unshift( $templates, 'archive-' . get_query_var( 'cat' ) . '.twig' );
} else if ( is_post_type_archive() ) {
$context['title'] = post_type_archive_title( '', false );
array_unshift( $templates, 'archive-' . get_post_type() . '.twig' );
}
$context['posts'] = Timber::get_posts();
Timber::render( $templates, $context );
In my home.twig file where all post are display :
<ul class="category--list">
{% for cat in categories %}
<li><a href="{{cat.link}}" class="h-cat">{{cat.name}}</a></li>
{% endfor %}
</ul>
What loop must I create in my "archive.twig" file to only display post in a term clicked ?
@Jardon, first step is to get the relevant term...
archive.php
$context['term'] = new Timber\Term();
//this will fetch whichever term is in the scope of that php file
then you just need to send it over to the twig file for display...
archive.twig
<h2>{{ term.name }}</h2>