获取分类法的固定链接

我有一个自定义帖子类型的硬件,附加了一个名为 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>

我尝试了很多其他的东西,但我似乎无法获得类别链接......


谁能告诉我哪里错了?感谢您的关注。


蛊毒传说
浏览 100回答 1
1回答

神不在的星期二

你能试试这个吗:$term_link = get_term_link( $term->term_id, "hardware_categories" );还可以尝试修改您的条款:$terms = get_terms( array(&nbsp; &nbsp; 'taxonomy' => 'taxonomy_name',&nbsp; &nbsp; 'hide_empty' => false) );您使用 get_terms 的方式已被弃用,请参阅文档你的代码应该是这样的$terms = get_terms( array(&nbsp; &nbsp; 'taxonomy' => 'hardware_categories',&nbsp; &nbsp; 'hide_empty' => false) );foreach( $terms as $term ):?>&nbsp; &nbsp; <h2><a href="<?php echo get_term_link( $term->term_id, 'hardware_categories');?>"><?php echo $term->name; ?></a></h2><?php endforeach;?>&nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP