循环以显示分类术语和描述


loop to display taxonomy terms and description

因此,我正在尝试构建导航层次结构中的第一个页面来选择产品类别。

我有一个名为集合的自定义分类法。我想创建一个页面,循环浏览集合分类法中的所有术语,并显示Term+链接和描述。

我创建了一个名为taxonomy-collections.php的文件,并放入了以下代码。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'collections' );
echo '<ul>';
foreach ( $terms as $term ) {
    // The $term is an object, so we don't need to specify the $taxonomy.
    $term_link = get_term_link( $term );
    // If there was an error, continue to the next term.
    if ( is_wp_error( $term_link ) ) {
        continue;
    }
    // We successfully got a link. Print it out.
    echo '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
    echo  term_description($post->ID,$term);
    }
echo '</ul>';
?>
<?php endwhile; else : ?>
    <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

这几乎是直接从编码中得到的,所以我猜我遗漏了一些东西。

目前,代码中的每个术语都显示为列表,但如果有人也能帮助的话,我确实想把它改成网格格式。

因此,每个术语和描述都将封装在一个div中,并与下一个右对齐。

感谢

这样?

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'collections' );
foreach ( $terms as $term ) {
  // The $term is an object, so we don't need to specify the $taxonomy.
  $term_link = get_term_link( $term );
  // If there was an error, continue to the next term.
  if ( is_wp_error( $term_link ) ) {
    continue;
  }
  // We successfully got a link. Print it out.
  echo '<div><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
  echo  term_description($post->ID,$term).'</div>';
}
?>
<?php endwhile; else : ?>
  <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>