简体   繁体   English

带术语的木材插件(WordPress)

[英]Timber plugin with term (WordPress)

I have a list of posts with one term assigned for one post. 我有一个帖子列表,其中一个术语分配给一个帖子。

I have 5 terms. 我有5个学期。 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 : 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 : 在我的home.twig文件中,其中显示了所有帖子:

<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 ? 我必须在“ archive.twig”文件中创建什么循环才能仅显示单击项中的帖子?

@Jardon, first step is to get the relevant term... @Jardon,第一步是获取相关术语...

archive.php 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM