首页 文章

循环显示分类术语和描述

提问于
浏览
0

所以我试图在导航层次结构中构建第一页以选择产品类别 .

我有一个名为collections的自定义分类 . 我想创建一个循环遍历集合分类中所有术语的页面,并显示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中,并与下一个对齐 .

谢谢

1 回答

  • 0

    像这样?

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

相关问题