在 WordPress 中使用 Timber/Twig 将父子类别与帖子和显示分组在一起 [英] Group parent and child categories together with posts and display using Timber/Twig in WordPress

查看:27
本文介绍了在 WordPress 中使用 Timber/Twig 将父子类别与帖子和显示分组在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 WordPress 查询代码在我的 Timber 主题中工作,但我在如何转换为 Timber/Twig 格式方面苦苦挣扎.

$args = 数组('分类' =>'类别','父母' =>'7','orderby' =>'姓名','订单' =>'ASC','hide_empty' =>错误的,);$terms = get_terms( $args );foreach ( $terms 作为 $term ) {$termId = $term->term_id;//输出父类别 ID 7 的第一级子级回声'<p>'.$term->name .'</p>';$args = 数组('分类' =>'类别','child_of' =>$termId,'orderby' =>'姓名','订单' =>'ASC','hide_empty' =>错误的,);$childTerms = get_terms( $args );foreach ( $childTerms as $childTerm ) {$childTermId = $childTerm->term_id;//输出父类别 ID 7 的第二级子级回声'<p>'.$childTerm->name .'</p>';$args = 数组('猫' =>$childTermId,'orderby' =>'标题','订单' =>'ASC','posts_per_page' =>-1,);$query = new WP_Query( $args );while( $query->have_posts() ) : $query->the_post();//输出分配给二级子类别的帖子echo '<p><a href="'.get_the_permalink() .'>'.get_the_title() .'</a></p>';终了;wp_reset_postdata();//$posts = Timber::get_posts( $args );}}

功能不完整的 Timber/Twig 代码示例

{% for term in terms %}<div class="category"><h3>{{ term.name }}{% for childTerm %}{% if childTerm.parent == term.term_id %}<div class="category__child"><h4>{{ childTerm.name }}</h4><!-- 从这里输出子术语的帖子-->

{% 万一 %}{% 结束为 %}

{% 结束为 %}

HTML 嵌套输出示例

父类别

  • 儿童类别
    • 帖子标题和摘录
    • 帖子标题和摘录
    • 帖子标题和摘录
  • 儿童类别
    • 帖子标题和摘录
    • 帖子标题和摘录
    • 帖子标题和摘录

父类别

  • 儿童类别
    • 帖子标题和摘录
    • 帖子标题和摘录
    • 帖子标题和摘录

父类别

  • 儿童类别
    • 帖子标题和摘录
    • 帖子标题和摘录
    • 帖子标题和摘录

非常感谢任何帮助.

解决方案

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

<?php$数据 = [];$terms = get_terms(['分类' =>'类别','父母' =>'7','orderby' =>'姓名','订单' =>'ASC','hide_empty' =>错误的,]);foreach ($terms as $term) {/*** 将父项分配给数组并启动子数组* 使用term id,这样你就可以更容易地将孩子与他们的父母配对**/$data[$term->term_id] = ['名称' =>$term->name,'孩子' =>[],];$childTerms = get_terms(['分类' =>'类别','child_of' =>$term->term_id,'orderby' =>'姓名','订单' =>'ASC','hide_empty' =>错误的,]);foreach ($childTerms as $childTerm) {/*** 将子项分配给数组内的父项并启动 post 数组*使用子术语ID,以便您可以更轻松地将帖子与正确的子项匹配**/$data[$term->term_id]['children'][$childTerm->term_id] = ['名称' =>$childTerm->name,'帖子' =>[],];$query = new WP_Query(['猫' =>$childTerm->term_id,'orderby' =>'标题','订单' =>'ASC','posts_per_page' =>-1,]);while($query->have_posts()) {$query->the_post();$data[$term->term_id]['children'][$childTerm->term_id]['posts'][] = ['网址' =>get_the_permalink(),'标题' =>get_the_title(),];}wp_reset_postdata();}}

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

    {% for parent in data %}<li>{{ 父母名字 }}{% if parent.children|default %}<ul>{% for child in parent.children %}<li>{{ child.name }}{% if child.posts|默认 %}<ul>{% 用于 child.posts 中的帖子 %}<li><a href={{ post.url }}";title={{ post.title }}">{{ post.title }}</a></li>{% 结束为 %}
{% 万一 %}{% 结束为 %}{% 结束为 %}{% 结束为 %}

演示


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

I have the following WordPress query code working in my Timber theme, but am struggling with how to convert into the Timber/Twig format.

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

Example Timber/Twig code with incomplete functionality

{% 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 nested output example

Parent Category

  • Child Category
    • Post Title and excerpt
    • Post Title and excerpt
    • Post Title and excerpt
  • Child Category
    • Post Title and excerpt
    • Post Title and excerpt
    • Post Title and excerpt

Parent Category

  • Child Category
    • Post Title and excerpt
    • Post Title and excerpt
    • Post Title and excerpt

Parent Category

  • Child Category
    • Post Title and excerpt
    • Post Title and excerpt
    • Post Title and excerpt

Any assistance is greatly appreciated.

解决方案

First you have to alter the data your are sending to the view. Group the children with the parents and groups the posts with the corresponding child. This could be achieved with something like this:

<?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();
        }
    }

This will create a nested array which will be easier to use inside twig, e.g.

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

demo


note: didn't test the wordpress part as I don't use wordpress

这篇关于在 WordPress 中使用 Timber/Twig 将父子类别与帖子和显示分组在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆