WordPress循环问题从导航中的自定义分类结构获取链接

WordPress loop issue getting links from custom taxonomy structure in navigation

我正在尝试创建一个动态循环来让自定义分类类别显示在导航中。我已经使用此代码注册了我的自定义 post 类型“服务”:

<?php 
function asb_post_types(){
    register_post_type('services', array(
        // remove editor so it takes away default content field
        'supports' => array ('title','excerpt', 'editor', 'thumbnail'),
        'rewrite' => array(
            // CHANGES ARCHIVE LINK
            'slug' => 'services'
        ),
        // create the archive for events
        'has_archive' => true,
        // makes visible to editors & previews of the site
        'public' => true,
        // tell it what icon to use
        'menu_icon' =>  'dashicons-admin-appearance',
        'taxonomies' => array('post_tag'),
        'labels' => array(
            // display name for admin panel
            'name' => 'Services',
            'add_new_item' => 'Add New Service',
            'edit_item' => "Edit Services",
            'all_items' => "All Services",
            'singular_name' => "service"
        )
    ));

}   

add_action('init', 'asb_post_types');
?>

在我的 functions.php 中,我使用以下代码注册了自定义分类法:

 function service_category_taxonomy() {
 
 
  $labels = array(
    'name' => _x( 'Service Category', 'taxonomy general name' ),
    'singular_name' => _x( 'service category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Service Categories' ),
    'all_items' => __( 'All Service Categories' ),
    'parent_item' => __( 'Parent Service Categories' ),
    'parent_item_colon' => __( 'Parent Service Category:' ),
    'edit_item' => __( 'Edit Service Categories' ), 
    'update_item' => __( 'Update Service Categories' ),
    'add_new_item' => __( 'Add New Service Category' ),
    'new_item_name' => __( 'New Service Categories Name' ),
    'menu_name' => __( 'Service Category' ),
  );    
 
// Now register the taxonomy
  register_taxonomy('service categories',array('services'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_in_rest' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'public'   => true,
    'exclude_from_search' => false,
    'rewrite' => array( 'slug' => 'service_category' ),
  ));
 
}

add_action( 'init', 'service_category_taxonomy', 0 );

在我的导航中,我使用此循环从分类法中调用特定类别:

<?php 
            $terms = get_terms(
              array(
                  'taxonomy'   => 'service categories',
                  'hide_empty' => true,
              )
          );

          // Check if any term exists
          if ( ! empty( $terms ) && is_array( $terms ) ) {
              // add links for each category
              foreach ( $terms as $term ) { ?>
                <li class="nav-item">
                  <a class="nav-link" href="<?php echo esc_url( get_term_link( $term ) ) ?>">
                      <?php echo $term->name; ?>
                  </a>
                </li>
              <?php
              }
          }
          ?>

我已经刷新了我的固定链接,当我将鼠标悬停在链接上时,它会生成一个 URL 应该可以工作,但是当你点击它时,它只会刷新前面 - page.php模板。我的博客存档和索引页面在博客上正常工作,所以我有点不知所措,为什么只有它不能按预期运行。感谢任何帮助。

经过一些研究,WordPress 只允许使用小写字母、破折号和下划线作为自定义分类名称。我将其更改为新名称,并且效果很好。希望这对以后的人有所帮助!