4

I'm trying to figure out how to display a list of specific taxonomy (categorycourses).

In functions.php for the theme I have:

//Register custom taxonomy for courses-categories
$course_cat_args = array(
  'hierarchical' => true,
  'labels' => $course_cat_labels, //Other labels set before
  'show_ui' => true,
  'show_admin_column' => true,
  'query_var' => true,
  'rewrite' => array( 'hierarchical' => true )
);

register_taxonomy( 'categorycourses', array('course'), $course_cat_args );

The actual taxonomy is working fine in admin.

Now I'm trying to create a template for categorycourses at frontend that lists all coursecategories.

I've looked at the template hierarchy and I thought I should create a file with the taxonomy name categorycourses.php

So I created a file called categorycourses.php in the themes folder. I also tried creating categorycourses-categorycourse.php, archive-categorycourses.php but I just keep getting a 404 error.

Please bare with me. I understand lot of backend-stuff in WP, but I'm just learning how to create templates at frontend.

I'm able to create archive-pages for certain post typs, so rewrites rules and so on are working on my local server.

What am I doing wrong?

UPDATE 1. I've tried to create a file name taxonomy-categorycourses.php and with same result (404 error) I've flushed permalink-settings in admin by resaving them

I've also tried:

$course_cat_args = array(
  'hierarchical' => true,
  'labels' => $course_cat_labels,
  'show_ui' => true,
  'show_admin_column' => true,
  'query_var' => true,
  'rewrite' => array('slug' => 'soup')
);

register_taxonomy( 'categorycourses', array('course'), $course_cat_args );

I try to access the template with http://server/soup but still get the error. (after resaving permalink-settings in admin)

UPDATE2: *I think I've figured it out:* There don't seem to be any template for "root-taxonomy". You have to choose a specific coursecategory to act as a root.

In my case I've created this file:

taxonomy-categorycourses-semester2014.php

When I then access http://server/soup/semester2014 the expected template (taxonomy-categorycourses-semester2014.php) is shown.

But there seems no way to actually create a "root template for a taxonomy" like http://server/soup/ . Even if I create a template called taxonomy-categorycourses.php it does not work.

Is this correct?

4

1 Answer 1

5

What you want to do is impossible without a page.php type of template. There is no template hierarchy that support what you want to achieve. It works exactly the same with categories. taxonomy-categorycourses.php will not display a list of categorycourses, so would category-categorycourses.php if categorycourses was a normal category. If you click on categorycourses, you will be taken to a page that will display posts from that taxonomy or category.

If you need to show a list of terms or categories under a taxonomy, you will need to create a custom page.php template and use get_the_terms() to get a list of all the terms/categories attached to a taxonomy. wp_list_categories( $args ) will output the list, so you can modify this to display what you need. You can make a copy of your themes' page.php template and call it something like page-tax.php You need to change the loop in this template. Here is an example for the twentyfourteen theme.

 <?php
/**
 * Template Name: Page Tax
 */
get_header(); ?>

<div id="main-content" class="main-content">

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

        <?php //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)

            $taxonomy     = 'brands';
            $orderby      = 'name'; 
            $show_count   = 0;      // 1 for yes, 0 for no
            $pad_counts   = 0;      // 1 for yes, 0 for no
            $hierarchical = 1;      // 1 for yes, 0 for no
            $title        = '';

            $args = array(
              'taxonomy'     => $taxonomy,
              'orderby'      => $orderby,
              'show_count'   => $show_count,
              'pad_counts'   => $pad_counts,
              'hierarchical' => $hierarchical,
              'title_li'     => $title
            );

        ?>

        <ul>
            <?php wp_list_categories( $args ); ?>
        </ul>

        </div><!-- #content -->
    </div><!-- #primary -->
    <?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->

<?php
get_footer();

Just remember to change $taxonomy = 'brands'; to your taxonomy name. You can now create a new page in the "Add New Page" screen, set your page slug to the taxonomy name , and select this template. You can now enter http://server/categorycourses/ and you will be directed to this page you created.

Original Answer Your answer is right in the link you provided. The codex states it perfectly, you just need to implement it correctly. Your template should be called taxonomy-categorycourses.php. Please refer to the link you provided

UPDATE1 Hit me like a ton of bricks, had the same problem a while ago with a CPT, and I missed that when I looked at your code. For your custom template to work, you need to add 'has_archive' => true, to your arguments when registering your custom post type. For your info, go and check registering taxonomies and registering custom post types

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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