如何在循环分类法 wordpress 中显示 url 分类法?

how to display url taxonomy in loop taxonomy wordpress?

我有一个自定义分类法类型,我想在 genre.php 页面中显示所有分类法。当我尝试使用此代码时 php echo $term->slug;

结果不符合我的预期:

http:///localhost/site/action

我想要这样的结果:

http:///localhost/site/genre/action

不知道哪里出错了,一​​直在找,没找到解决方法

这是我的代码genre.php

<?php
/*
Template Name: Genre
*/
get_header(); ?>
<div class="content">
  <div class="main-content">
    <div class="main-container">
      <div id="list_categories_categories_list">
        <?php get_template_part( 'template-parts/ads-bottom' ); ?>
        <div class="headline">
          <h1>
            Genre                   
          </h1>
        </div>
        <div class="box">
          <div class="list-categories">
            <div class="margin-fix" id="list_categories_categories_list_items">
              <?php
                $terms = get_terms( array(
                    'taxonomy' => 'genre',
                    'hide_empty' => false,
                    'number' => 20
                ) );
                
                foreach ($terms as $term){ ?>
                <?php $image = get_term_meta( $term->term_id, 'image', true ); ?>
                <a class="item" href="<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
                    <div class="img">
                    <?php if ( $image != '' ) {
                        echo wp_get_attachment_image( $image, "", ["class" => "thumb"]);
                    }
                    ?>
                    </div>
                    <strong class="title"><?php echo $term->name; ?></strong>
                    <div class="wrap">
                    <div class="videos">0 videos</div>
                    <div class="rating positive">
                        81%
                    </div>
                    </div>
                </a>
              <?php } ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<?php 
get_footer();

您可以使用此代码,它将根据您的要求给出结果。

<?php
/*
Template Name: Genre
*/
get_header(); ?>
<div class="content">
  <div class="main-content">
    <div class="main-container">
      <div id="list_categories_categories_list">
        <?php get_template_part( 'template-parts/ads-bottom' ); ?>
        <div class="headline">
          <h1>
            Genre                   
          </h1>
        </div>
        <div class="box">
          <div class="list-categories">
            <div class="margin-fix" id="list_categories_categories_list_items">
              <?php
                $terms = get_terms( array(
                    'taxonomy' => 'genre',
                    'hide_empty' => false,
                    'number' => 20
                ) );
                
                foreach ($terms as $term){ ?>
                <?php $image = get_term_meta( $term->term_id, 'image', true ); ?>
                <a class="item" href="<?php echo get_term_link($term->term_id); ?>" title="<?php echo $term->name; ?>">
                    <div class="img">
                    <?php if ( $image != '' ) {
                        echo wp_get_attachment_image( $image, "", ["class" => "thumb"]);
                    }
                    ?>
                    </div>
                    <strong class="title"><?php echo $term->name; ?></strong>
                    <div class="wrap">
                    <div class="videos">0 videos</div>
                    <div class="rating positive">
                        81%
                    </div>
                    </div>
                </a>
              <?php } ?>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<?php 
get_footer();?>