Php 使用WordPress中的木材/细枝将父类别和子类别与帖子一起分组并显示

Php 使用WordPress中的木材/细枝将父类别和子类别与帖子一起分组并显示,php,wordpress,twig,timber,Php,Wordpress,Twig,Timber,我有下面的WordPress查询代码在我的木材主题中工作,但是我正在努力转换成木材/细枝格式 $args = array( 'taxonomy' => 'category', 'parent' => '7', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, ); $terms = get_terms( $args ); foreach ( $terms as $term

我有下面的WordPress查询代码在我的木材主题中工作,但是我正在努力转换成木材/细枝格式

$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 );
  }
}
$args=array(
“分类法”=>“类别”,
“父项”=>“7”,
'orderby'=>'name',
“订单”=>“ASC”,
“hide_empty”=>false,
);
$terms=get_terms($args);
foreach($terms作为$term){
$termId=$term->term\U id;
//输出父类别ID 7的第一级子级
回显“”.$term->name.“

”; $args=数组( “分类法”=>“类别”, 'child_of'=>$termId, 'orderby'=>'name', “订单”=>“ASC”, “hide_empty”=>false, ); $childTerms=get_terms($args); foreach($childTerms作为$childTerm){ $childTermId=$childTerm->term\u id; //输出父类别ID 7的第二级子级 回显“”.$childTerm->name.“

”; $args=数组( 'cat'=>$childTermId, 'orderby'=>'title', “订单”=>“ASC”, “每页帖子数”=>-1, ); $query=新的WP\u查询($args); 而($query->have_posts()):$query->the_post(); //分配给二级子类别的输出职位 回声“

”; 结束时; wp_reset_postdata(); //$posts=木材::获取_posts($args); } }
功能不完整的木材/树枝代码示例

{% 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 %}
{%表示术语%}
{{term.name}
{以%表示的childTerm的百分比}
{%if childTerm.parent==term.term\u id%}
{{childTerm.name}
{%endif%}
{%endfor%}
{%endfor%}
HTML嵌套输出示例

父类别

  • 子类别
    • 文章标题和摘录
    • 文章标题和摘录
    • 文章标题和摘录
  • 子类别
    • 文章标题和摘录
    • 文章标题和摘录
    • 文章标题和摘录
父类别

  • 子类别
    • 文章标题和摘录
    • 文章标题和摘录
    • 文章标题和摘录
父类别

  • 子类别
    • 文章标题和摘录
    • 文章标题和摘录
    • 文章标题和摘录

非常感谢您的帮助。

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


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

页面故事PHP

$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 );
页面故事细枝

{% 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 %}
{%用于数据%中的父对象]
{{parent.name}
{%if parent.children | default%}
{parent.children%}
{{child.name}
{%if child.posts | default%}
{child.posts%%中的post为%s}
{{post.date}
{%endfor%}
{%endif%}
{%endfor%}
{%endif%}
{%endfor%}