0

I'm currently learning Wordpress.

So my theme has a custom post type called portfolio. I'm using this online code to add a custom taxonomy to this portfolio custom type:

function add_custom_taxonomies() {
  // Add new "Locations" taxonomy
  register_taxonomy('location', 'portfolio_page', array(
    // Hierarchical taxonomy (like categories)
    'hierarchical' => true,
    // This array of options controls the labels displayed in the WordPress Admin UI
    'labels' => array(
      'name' => _x( 'Locations', 'taxonomy general name' ),
      'singular_name' => _x( 'Location', 'taxonomy singular name' ),
      'search_items' =>  __( 'Search Locations' ),
      'all_items' => __( 'All Locations' ),
      'parent_item' => __( 'Parent Location' ),
      'parent_item_colon' => __( 'Parent Location:' ),
      'edit_item' => __( 'Edit Location' ),
      'update_item' => __( 'Update Location' ),
      'add_new_item' => __( 'Add New Location' ),
      'new_item_name' => __( 'New Location Name' ),
      'menu_name' => __( 'Locations' ),
    ),
    // Control the slugs used for this taxonomy
    'rewrite' => array(
      'with_front' => false, // Don't display the category base before "/locations/"
      'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
    ),
  ));
}
add_action( 'init', 'add_custom_taxonomies', 0 );

So above code successfully added the sub menu locations under portfolio. I've created a template file inside my theme and name it as taxonomy-location.php with this very simple code just for testing purpose:

<?php

    echo 'test';

However when I access above locations tab, the content is always like Add New Summary with some fields like name, slug, description just like how you add a new category or tag, but what I want to see is the word test from the template that I've created just now. So what's wrong with my approach that Wordpress cannot display the correct template?

1

A hierarchical custom taxonomy works is the same exact way as would the build-in taxonomy category. On the other hand, non hierarchical taxonomies would work exactly like the build-in taxonomy post_tag

You have to add terms to your taxonomy in the same way as you would add "categories" in the category screen back end.

The template taxonomy-locations.php would be used if any term that belongs to the taxonomy locations are selected or are being viewed.

You have to remember, just like the built-in taxonomy category, you cannot access the taxonomy name directly, this will immediately lead to a 404 been showed as there are no existing index pages for taxonomies

  • Thanks for your answer but how can I change the default content of the custom taxonomy. I can see many of plugins can put the custom content in the submenu items of their custom post type. Is there any difference between adding custom taxonomy for plugin and adding custom taxonomy for theme? (Please feel free to edit your answer to clarify my above question instead of reply here) – Resolution Dec 4 '14 at 8:54
  • I don't understand what you need to do. Please file an edit and add a screenshot of what you are trying to accomplish or add a proper example. A taxonomy is the same, whether it was added by a theme or plugin. Best practice, taxonomies and post types should be added by to plugins, not themes – Pieter Goosen Dec 4 '14 at 9:26
  • For example, if you access this demo from wp-robot: test.wprobot.net/m2pozcjjfwk1zdv/wp-admin. As you can see, when you click campaigns they can put custom content there. Currently when I click locations under my portfolio tab, I always see add new location and some fields as I've described in my question, what I'm trying to do is to put some custom contents there. – Resolution Dec 4 '14 at 9:51
  • I cannot access the link. Do you mean that when you visit the location admin page, you want to see terms already there? I really don't understand "put some custom contents there". In that page you need to add terms in the same way you add categories. What custom content do you need to add. – Pieter Goosen Dec 4 '14 at 10:13
  • You can access the demo from here: test.wprobot.net. I just want to add any kind of content beside the default add new item of wordpress custom taxonomy. Something like display a blank page with a single word test is the simplest example come into my mind. – Resolution Dec 4 '14 at 10:47
0

If you don't see template content after create new taxonomy or cpt maybe you could try to refresh your permalinks settings. Wordpress need to crawl for all new items in your system and create new (ie: portfolio, location) url slugs for templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.