Support » Fixing WordPress » List a custom taxonomy's terms, with links, on the taxonomy page

  • I have registered a custom post type in my functions.php file for “Wines”, with this code:

    function customposttype_wines() {
    $labels = array(
        'name'                => 'Wines',
        'singular_name'       => 'Wine',
        'menu_name'           => 'Wines',
        'parent_item_colon'   => 'Parent Wine',
        'all_items'           => 'All Wines',
        'view_item'           => 'View Wine',
        'add_new_item'        => 'Add New Wine',
        'add_new'             => '+',
        'edit_item'           => 'Edit Wine',
        'update_item'         => 'Update Wine',
        'search_items'        => 'Search Wines',
        'not_found'           => 'No Wines found',
        'not_found_in_trash'  => 'No Wines found in Trash',
    $args = array(
        'label'               => 'wines',
        'description'         => 'Wine Reviews',
        'labels'              => $labels,
        'supports'            => array( 'title', ),
        'unset'            => array ('date'),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'post',
    add_action( 'init', 'customposttype_wines', 0 );

    And registered 9 custom taxonomies (brand, region, grape, etc.) with this code (repeated 9 times with only the <XXX> changing for each):

    function customtaxonomy_<XXX> ()  {
    $labels = array(
        'name'                       => '<XXX>',
        'singular_name'              => '<XXX>',
        'menu_name'                  => '<XXX>s',
        'all_items'                  => 'All <XXX>s',
        'parent_item'                => 'Parent <XXX>',
        'parent_item_colon'          => 'Parent <XXX>:',
        'new_item_name'              => 'New <XXX> Name',
        'add_new_item'               => 'Add New <XXX>',
        'edit_item'                  => 'Edit <XXX>',
        'update_item'                => 'Update <XXX>',
        'separate_items_with_commas' => 'Separate <XXX>s w/commas',
        'search_items'               => 'Search <XXX>s',
        'add_or_remove_items'        => 'Add or Remove <XXX>s',
        'choose_from_most_used'      => 'Choose from the most used <XXX>s',
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => true,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'query_var'                  => true,
    add_action( 'init', 'customtaxonomy_<XXX>', 0 );

    Now I am trying to display the lists of custom taxonomy terms on the parent page of each, so when visiting<XXX&gt; there is a list, with links, to all the terms that have been entered.

    Right now when I visit<XXX&gt; I just see the 404.php error page. But if I visit a taxonomy term page I see the posts within there fine, ie:<XXX>/term

    I tried using a taxonomy template hierarchy page, taxonomy-<XXX>.php but it will not load, still just shows the 404.php.

    Pulling hair here, any help is very appreciated.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator keesiemeijer



    How did you get urls like this:<XXX>/term
    Custom taxonomies have urls like this:<XXX>/term

    Did you register the taxonomy with something like this:

    'rewrite' => array(
    	'slug' => 'wines/<XXX>',
    	'with_front' => true,
    	'hierarchical' => true ,
    ) ,

    Or are you using some other rewrite rules?

    Did you re-save your permalink structure in wp-admin > Settings >Permalinks.

    @keesiemeijer – oh, yes, I am using this plugin to have the /wines in the links because I couldn’t figure out how to do it otherwise. I’ll try disabling the plugin and adding the rewrites as you have listed to see if that helps. I will report back. Thank you for your help.

    No luck 🙁 The new rewrite argument is making a 404 appear now at<XXX>/term instead of the posts that were loading before.

    Oh, and yes I did flush the rewrite after the change by going to Permalinks > Settings and clicking save.

    Moderator keesiemeijer



    Try it without the plugin (deactivate and refresh permalinks) and register the custom taxonomies with the rewrite rules as shown above:

    'rewrite' => array(
    	'slug' => 'wines/<XXX>',
    	'with_front' => true,
    	'hierarchical' => true ,
    ) ,

    Now your terms should be at<XXX>/term

    Just refresh the permalink structure and see where WordPress thinks the term archive pages are when visiting the custom terms in the wp-admin.

    Create a ‘wine’ custom post type post called “<XXX>”
    this post has a url<XXX>

    Do this for all your taxonomies.

    Create a template for the single custom post type display:
    single-{post_type}.php (single-wines.php)

    In this template check the ID of the post and show the terms.
    Very basic example:

    $taxonomy = '';
    // $post ID is available on single post pages
    if($post->ID == 22)
    	$taxonomy = 'grapes';
    if($post->ID == 44)
    	$taxonomy = 'brand';
    // etc
    // check if it's a taxonomy post
    if('' != $taxonomy) {
    	// show all terms from the taxonomy
    	 wp_list_categories( 'taxonomy=' . $taxonomy ); 
    } else {
    	// normal custom post type wine post
    	// single loop here

    Always re-save your permalink structure after every step. I hope this all makes sense. I have this setup working on a couple of sites of mine.

    Thank you again, keesiemeijer. The problem I see there is that my custom post type is “wines” and I would like the posts to appear under it as-is:

    Just to clarify the goal:

    • @ = all posts
    • @ = all posts under the custom post type “wines”
    • @ = a list of all the terms that have been created under the custom taxonomy “brands”, which is a taxonomy linked to the custom post type “wines”.
    • @ = all the posts that have the term “acme” applied to them, which is a term belonging to the custom taxonomy “brands”.

    The only one not working right now is the 3rd bullet, @ is where I get a 404. Shouldn’t that page load the taxonomy.php or if it doesn’t exist load the index.php?

    Moderator keesiemeijer



    Did you create a wines custom post type post ‘brands’?
    What theme are you using?

    No, “brands” is one of the custom taxonomies.

    I’m building the theme from scratch, so it’s very simple and only using index.php and single.php for displaying the posts. I’m obviously doing something wrong or thinking I can do something that I cannot, perhaps.

    The custom taxonomies are hierarchical, so they show in the admin like categories do for regular posts (with check boxes beside each). So, for the BRANDS taxonomy, “acme” is the category checked/selected under it.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘List a custom taxonomy's terms, with links, on the taxonomy page’ is closed to new replies.