2016-12-28 30 views
0

我有一篇文章的列表,其中有一篇文章分配給了一篇文章。Timber plugin with term(WordPress)

我有5個術語。我生成了所用術語的列表。我只想在我的存檔頁面中顯示與該術語相同的帖子。

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); 

在我home.twig文件,所有職位都顯示:

<ul class="category--list"> 
    {% for cat in categories %} 
    <li><a href="{{cat.link}}" class="h-cat">{{cat.name}}</a></li> 
    {% endfor %} 
</ul> 

我必須在我的 「archive.twig」 創建循環什麼文件只顯示文章中點擊的文章?

回答

0

@Jardon,第一步是獲取相關的術語......

archive.php

$context['term'] = new Timber\Term(); 
//this will fetch whichever term is in the scope of that php file 

,那麼你只需要通過將其發送至樹枝文件顯示.. 。

archive.twig

<h2>{{ term.name }}</h2>