在 WordPress 中获取分类法类别的 URL

Get the URL of the taxonomy's category in WordPress

我需要帮助来检索分类法中类别的 URL。我有一个名为 'leagues' 的分类法,它显示类别的图像和名称,我需要添加 link 以便人们在单击名称时可以转到该类别。这是我下面的代码:

<ul>
                            <?php
                                $taxonomies = get_terms( array(
                                   'taxonomy' => 'leagues',
                                   'orderby' => 'name',
                                   'show_count' => 0,
                                   'pad_counts' => 0,
                                   'hierarchical' => 1,
                                ) );

                                foreach( $taxonomies as $category ) {
                                   if( $category->parent == 0 ) {
                                       $cat_id = $category->term_id;
                                       $logos = get_field('logo', 'term_' . $cat_id);
                                       //for image return format: Image Array
                                       $logo = $logos['sizes']['thumbnail']; //default WP image sizes: thumbnail, medium, large
                                       if ($logo) {
                                           echo '<li>';
                                               echo '<img src="';
                                               echo $logo;
                                               echo '" class="w-25 mr-4">';
                                               echo '<a href="';
                                               echo '#';
                                               echo '">';
                                               echo $category->name;
                                               echo '</a>';
                                           echo '</li>';
                                       }
                                   }
                                }
                            ?>
                        </ul>

我认为您要查找的函数是 get_category_link()

由于您在循环中获取术语对象 ($category),因此您可以使用 get_term_link() https://developer.wordpress.org/reference/functions/get_term_link/

get_term_link( $category->term_id )

echo '<a href="';
echo get_term_link( $category->term_id );
echo '">';