获取分类法的固定链接
Get Permalink for Taxonomy
我有一个自定义 post 类型的硬件,它附加了一个名为 hardware_categories 的自定义分类法。
我有一段代码将每个分类输出为 H3,并在每个分类下输出该分类内的硬件项目。
我无法在分类法名称周围添加永久link link。
我正在尝试的代码在这里:
<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>
我为尝试生成 link 添加的部分是这两行:
$term_link = get_term_link( $term );
和
<h2><a href="<?php esc_url( $term_link ) ?>"><?php echo $term->name; ?></a></h2>
我尝试了很多其他的东西,但我似乎无法获得类别 link...
谁能告诉我哪里出错了?感谢观看。
你能试试这个吗:
$term_link = get_term_link( $term->term_id, "hardware_categories" );
也尝试修改您的条款:
$terms = get_terms( array(
'taxonomy' => 'taxonomy_name',
'hide_empty' => false
) );
您使用 get_terms 的方式已弃用,请参阅 documentation
您的代码应如下所示
$terms = get_terms( array(
'taxonomy' => 'hardware_categories',
'hide_empty' => false
) );
foreach( $terms as $term ):?>
<h2><a href="<?php echo get_term_link( $term->term_id, 'hardware_categories');?>"><?php echo $term->name; ?></a></h2>
<?php endforeach;?>
我有一个自定义 post 类型的硬件,它附加了一个名为 hardware_categories 的自定义分类法。
我有一段代码将每个分类输出为 H3,并在每个分类下输出该分类内的硬件项目。
我无法在分类法名称周围添加永久link link。
我正在尝试的代码在这里:
<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>
我为尝试生成 link 添加的部分是这两行:
$term_link = get_term_link( $term );
和
<h2><a href="<?php esc_url( $term_link ) ?>"><?php echo $term->name; ?></a></h2>
我尝试了很多其他的东西,但我似乎无法获得类别 link...
谁能告诉我哪里出错了?感谢观看。
你能试试这个吗:
$term_link = get_term_link( $term->term_id, "hardware_categories" );
也尝试修改您的条款:
$terms = get_terms( array(
'taxonomy' => 'taxonomy_name',
'hide_empty' => false
) );
您使用 get_terms 的方式已弃用,请参阅 documentation
您的代码应如下所示
$terms = get_terms( array(
'taxonomy' => 'hardware_categories',
'hide_empty' => false
) );
foreach( $terms as $term ):?>
<h2><a href="<?php echo get_term_link( $term->term_id, 'hardware_categories');?>"><?php echo $term->name; ?></a></h2>
<?php endforeach;?>