WP -通过自定义字段创建嵌套列表


WP - Create a nested list by custom field

现在我有一个作者列表,在他们的名字旁边有他们的文章标题:

ie。1 -现在看起来像这样(注意B的名字被列出了两次)
名称-标题1
B名称-标题1
B名-标题2
C name - title 1

(代码是这样的)

<?php 
$posts = get_posts(array(
   'posts_per_page'  => -1,
   'post_type'       => 'post',
   'order'           => 'ASC',
   'orderby'        => 'author-last',
   'meta_key'        => 'author-last'
));
if( $posts ): ?>
   <ul>
   <?php foreach( $posts as $post ): setup_postdata( $post ) ?>
      <div>
         <?php echo get_post_meta($post->ID, 'author-last', true);?>
         <?php the_title(); ?>
      </div>
   <?php endforeach; ?>
   </ul>
   <?php wp_reset_postdata(); ?>
<?php endif; ?>

ie。2 -但是我需要这个列表像这样显示....


名称标题1
才能B名称
标题1
才能标题才能2
C的名字标题1才能

所以我通常希望有一个嵌套的列表,而不重复名称。因此,如果同一作者写了多个标题,它会像例2那样列出自己。

我需要在我的代码中改变什么?

可能

  <ul>
       <?php  
       foreach( $posts as $post ): setup_postdata( $post ) ?>
          <li> 
          <div>
          <?php
             static $curr_auth_name = '';
             if ($curr_auth_name != get_post_meta($post->ID, 'author-last', true)) {
                 $curr_auth_name = get_post_meta($post->ID, 'author-last', true);
                 echo $curr_auth_name;
             }
             echo the_title(); 
          ?>
          </div>
          </li> 
       <?php endforeach; ?>
  </ul>