首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WordPress中使用Timber/Twig组合父类别和子类别以及发布和显示

问在WordPress中使用Timber/Twig组合父类别和子类别以及发布和显示
EN

Stack Overflow用户
提问于 2021-02-12 04:13:32
回答 2查看 810关注 0票数 0

下面的WordPress查询代码在我的Timber主题中工作,但我很难将其转换为Timber/Twig格式。

代码语言:javascript
复制
$args = array(
  'taxonomy' => 'category',
  'parent' => '7',
  'orderby' => 'name',
  'order' => 'ASC',
  'hide_empty' => false,
);
$terms = get_terms( $args );

foreach ( $terms as $term ) {
  $termId = $term->term_id;

  // Output first level of children of parent category ID 7
  echo '<p>' . $term->name . '</p>';

  $args = array(
    'taxonomy' => 'category',
    'child_of' => $termId,
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => false,
  );
  $childTerms = get_terms( $args );

  foreach ( $childTerms as $childTerm ) {
    $childTermId = $childTerm->term_id;

    // Output second level of children of parent category ID 7
    echo '<p>' . $childTerm->name . '</p>';

    $args = array(
      'cat' => $childTermId,
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page' => -1,
    );

    $query = new WP_Query( $args );
    while( $query->have_posts() ) : $query->the_post();
      // Output posts assigned to second level children categories
      echo '<p><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></p>';
    endwhile;
    wp_reset_postdata();

    // $posts = Timber::get_posts( $args );
  }
}

不完全功能的示例木材/Twig代码()

代码语言:javascript
复制
{% for term in terms %}
<div class="category">
  <h3>
    {{ term.name }}
  </h3>
  {% for childTerm in terms %}
    {% if childTerm.parent == term.term_id %}
    <div class="category__child">
      <h4>{{ childTerm.name }}</h4>
      <!-- Output posts from child terms here -->
    </div>
    {% endif %}
  {% endfor %}
</div>
{% endfor %}

HTML嵌套输出示例

父类

  • 子类别
    • 帖子标题和节选
    • 帖子标题和节选
    • 帖子标题和节选

  • 子类别
    • 帖子标题和节选
    • 帖子标题和节选
    • 帖子标题和节选

父类

  • 子类别
    • 帖子标题和节选
    • 帖子标题和节选
    • 帖子标题和节选

父类

  • 子类别
    • 帖子标题和节选
    • 帖子标题和节选
    • 帖子标题和节选

任何援助都是非常感谢的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-12 08:00:20

首先,您必须更改要发送到视图的数据。将孩子与父母一起分组,并将帖子与相应的孩子分组。这可以通过这样的方式来实现:

代码语言:javascript
复制
<?php
    $data = [];

    $terms = get_terms([
        'taxonomy' => 'category',
        'parent' => '7',
        'orderby' => 'name',
        'order' => 'ASC',
        'hide_empty' => false,
    ]);

    foreach ($terms as $term) {
        /**
        *  Assign parent term to array and initiate children array
        *  Use term id so you can match the children easier with their parent
        **/
        $data[$term->term_id] = [
            'name'      => $term->name,
            'children'  => [],
        ];

        $childTerms = get_terms([
            'taxonomy' => 'category',
            'child_of' => $term->term_id,
            'orderby' => 'name',
            'order' => 'ASC',
            'hide_empty' => false,
        ]);

        foreach ($childTerms as $childTerm) {
            /**
            *  Assign child term to parent inside array and initiate post array
            *  Use child term id so you can match the post easier with the correct child
            **/
            $data[$term->term_id]['children'][$childTerm->term_id] = [
                'name' => $childTerm->name,
                'posts' => [],
            ];

            $query = new WP_Query([
                'cat' => $childTerm->term_id,
                'orderby' => 'title',
                'order' => 'ASC',
                'posts_per_page' => -1,
            ]);

            while($query->have_posts()) {
                $query->the_post();
                $data[$term->term_id]['children'][$childTerm->term_id]['posts'][] = [
                    'url'   => get_the_permalink(),
                    'title' => get_the_title(),
                ];
            }
            wp_reset_postdata();
        }
    }

这将创建一个嵌套数组,更容易在树枝内部使用。

代码语言:javascript
复制
<ul>
{% for parent in data %}
    <li>
        {{ parent.name }}
        {% if parent.children|default %}
            <ul>
            {% for child in parent.children %}
                <li>
                    {{ child.name }}
                    {% if child.posts|default %}
                    <ul>
                    {% for post in child.posts %}
                        <li><a href="{{ post.url }}" title="{{ post.title }}">{{ post.title }}</a></li>
                    {% endfor %}
                    </ul>
                    {% endif %}
                </li>
            {% endfor %}
            </ul>
        {% endfor %}
    </li>
{% endfor %}
</ul>

演示

注意:没有测试wordpress部件,因为我不使用wordpress

票数 1
EN

Stack Overflow用户

发布于 2021-02-12 19:56:34

下面是我正在使用的PHP和Twig代码,以及@DarkBee提供的有用的解决方案。我希望这对为WordPress使用Timber的任何人都有帮助。

页面故事PHP

代码语言:javascript
复制
$context = Timber::context();
$timber_post = new Timber\Post();

$data = [];

$terms = get_terms([
  'taxonomy' => 'category',
  'parent' => '7',
  'orderby' => 'name',
  'order' => 'ASC',
  'hide_empty' => false,
]);

foreach ( $terms as $term ) {  
  /**
   *  Assign parent term to array and initiate children array
   *  Use term id so you can match the children easier with their parent
   **/
  $data[$term->term_id] = [
    'name' => $term->name,
    'slug' => $term->slug,
    'children' => [],
  ];

  $childTerms = get_terms([
    'taxonomy' => 'category',
    'child_of' => $term->term_id,
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => false,
  ]);

  foreach ( $childTerms as $childTerm ) {    
    /**
     *  Assign child term to parent inside array and initiate post array
     *  Use child term id so you can match the post easier with the correct child
     **/
    $data[$term->term_id]['children'][$childTerm->term_id] = [
      'name' => $childTerm->name,
      'posts' => [],
    ];

    $query = new WP_Query([
      'cat' => $childTerm->term_id,
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page' => -1,
    ]);

    while($query->have_posts()) {
      $query->the_post();
      $data[$term->term_id]['children'][$childTerm->term_id]['posts'][] = [
        'url' => get_the_permalink(),
        'title' => get_the_title(),
        'date' => get_the_date(),
      ];
    }
    wp_reset_postdata();
  }
}

$context['data'] = $data;
Timber::render( array( 'page-' . $timber_post->post_name . '.twig', 'page.twig' ), $context );

页面故事Twig

代码语言:javascript
复制
{% for parent in data %}
<div class="category">
  <h3 id="{{ parent.slug }}">
    {{ parent.name }}
  </h3>
  {% if parent.children|default %}
    {% for child in parent.children %}
      <div class="category__child">
        <h4>
          {{ child.name }}
        </h4>
        {% if child.posts|default %}
          {% for post in child.posts %}
          <div class="story">
            <a href="{{ post.url }}" title="{{ post.title }}">
              {{ post.title }}
            </a>
            <span>
              {{ post.date }}
            </span>
          </div>
          {% endfor %}
        {% endif %}
      </div>
    {% endfor %}
  {% endif %}
</div>
{% endfor %}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66166265

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档