WordPress 嵌套循环

WordPress Nested Loops

我从在线教程中获得了一些代码(见下文),该教程显示了按字母顺序排列的类别名称列表,然后, 在每个类别下方,该类别的 post 个标题列表。

有效,但我希望 post 标题也按字母顺序显示。目前只有类别名称是按字母顺序排列的 - 参见图片:

我在网上做了一些研究,我想我可能需要设置一个 'nested loop' - 但我不知道如何编辑我的代码来做到这一点。

希望有人能告诉我如何编辑代码以让类别名称和 post 标题按字母顺序显示。

这是我正在使用的代码:

// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
   // Display category name
   echo '<h2 class="post-title">' . $category->name . '</h2>';
   echo '<div class="post-list">';
   // WP_Query arguments
   $args = array(
   'cat' => $category->term_id,
   'orderby' => 'term_order',
 );

// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><a href="<?php the_permalink();?>"><?php the_title(); ?></a></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
   // Display category name
   echo '<h2 class="post-title">' . $category->name . '</h2>';
   echo '<div class="post-list">';
   // WP_Query arguments
   $args = array(
   'cat' => $category->term_id,   
   'order' => 'ASC',
   'orderby' => 'title',
 );

// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><a href="<?php the_permalink();?>"><?php the_title(); ?></a></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach

我们可以添加带标题的 orderby 参数。