我有一个自定义帖子类型的硬件,附加了一个名为 hardware_categories 的自定义分类法。
我有一段代码,将每个分类法输出为 H3,并在每个分类法下输出该分类法内的硬件项目。
我在围绕分类名称添加永久链接时遇到问题。
我正在尝试的代码在这里:
<div class="container hardware-archive-container">
<?php // Output all Taxonomies names with their respective items
$terms = get_terms('hardware_categories');
foreach(
$terms as $term ):
$term_link = get_term_link( $term );
?>
<h2><a href="<?php esc_url( $term_link ) ?>"><?php echo $term->name; ?></a></h2>
<div class="row justify-content-center hardware-archive-row">
<?php
$posts = get_posts(array(
'post_type' => 'hardware',
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'nopaging' => true, // to show all posts in this taxonomy, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this taxonmy
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
<div class="col-md-3">
<?php
$image = get_field( 'hardware_main_image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
<?php endforeach; ?>
</div>
<a href="#"><p>See all products from: <?php echo $term->name;?></p></a>
<?php endforeach; ?>
</div>
我添加的用于尝试生成链接的部分是这两行:
$term_link = get_term_link( $term );
和
<h2><a href="<?php esc_url( $term_link ) ?>"><?php echo $term->name; ?></a></h2>
我尝试了很多其他的东西,但我似乎无法获得类别链接......
谁能告诉我哪里错了?感谢您的关注。
神不在的星期二