【问题标题】:Timber plugin with term (WordPress)带有术语的木材插件(WordPress)
【发布时间】:2016-12-28 09:16:25
【问题描述】:

我有一个帖子列表,其中一个术语分配给一个帖子。

我有 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”文件中创建什么循环才能仅在点击的术语中显示帖子?

【问题讨论】:

    标签: wordpress twig timber


    【解决方案1】:

    @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>
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 1970-01-01
      • 2022-10-02
      • 1970-01-01
      • 2015-12-25
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 2015-04-29
      相关资源
      最近更新 更多