Skip to content
Advertisement

WordPress loop issue getting links from custom taxonomy structure in navigation

I am trying to create a dynamic loop to get the custom taxonomy categories to show in the navigation. I have registered my custom post type “services” with this code:

<?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');
?>

In my functions.php I registered the custom taxonomy with the following code:

 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 );

In my navigation, I am using this loop to call the specific categories from the taxonomy:

<?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
              }
          }
          ?>

I have flushed my permalinks, and when I hover my mouse over the links it generates a URL that should work, but when you click on it, it just refreshes the front-page.php template. My blog archive and index pages work properly for the blog so I am kind of at a loss why only this is not functioning as expected. Any help is appreciated.

Advertisement

Answer

After doing some research the WordPress only allows lowercase letters, dash, and underscore for the custom taxonomy name. I changed it to a new name and it worked perfectly. Hopefully, this helps someone in the future!

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement