为了账号安全,请及时绑定邮箱和手机立即绑定

获取分类法的固定链接

获取分类法的固定链接

PHP
蛊毒传说 2023-08-06 14:36:11
我有一个自定义帖子类型的硬件,附加了一个名为 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>我尝试了很多其他的东西,但我似乎无法获得类别链接......谁能告诉我哪里错了?感谢您的关注。
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

你能试试这个吗:


$term_link = get_term_link( $term->term_id, "hardware_categories" );

还可以尝试修改您的条款:


$terms = get_terms( array(

    'taxonomy' => 'taxonomy_name',

    'hide_empty' => false

) );

您使用 get_terms 的方式已被弃用,请参阅文档


你的代码应该是这样的


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


查看完整回答
反对 回复 2023-08-06
  • 1 回答
  • 0 关注
  • 71 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信