Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Jahan Zeb
Jahan Zeb
11,169 Points

Custom Taxonomy query display

I am expanding on the custom theme project we did at treehouse. I added taxonomy to custom post type (work) using Custom post type UI plugin called 'work_categories'

I am successfully displaying the term names by using get_terms() and foreach loop with the respective links.

<?php 
$args = array(
              'orderby' => 'name',
              'hide_empty' => false
              );
        $tax_terms = get_terms('work_categories', $args);

          foreach ($tax_terms as $tax_term) {
            // Sanitize the term, since we will be displaying it.
            $tax_term = sanitize_term($tax_term, 'work_categories');

            $term_link = get_term_link( $tax_term, 'work_categories' );
            $term_taxonomy = $tax_term->taxonomy;
            $term_slug = $tax_term->slug;
            $term_name = $tax_term->name;

            // If there was an error, continue to the next term.
            if ( is_wp_error( $term_link ) ) {
                continue;
            }

            echo '<li><a href="' . esc_url( $term_link ) . '">' . $term_name . ' (' . $tax_term->count . ')' . '</a></li>';

          } ?>

These links are directing me to a url like this which is fine:

http://localhost/Localwp/work_categories/cdm/

http://localhost/Localwp/work_categories/interactive/

http://localhost/Localwp/work_categories/video/

http://localhost/Localwp/work_categories/web/

http://s7.postimg.org/wiwx9tf57/Screen_Shot_2014_07_05_at_4_49_38_PM.jpg

I have tried making and renaming many archive/taxonomy php files (mentioned here) in my theme directory to possibly catch these links and display custom taxonomy content but nothing is working. It is always sending me to default index.php

Could someone help me or point me to the right direction that which php file these links are being directed to??

Any help will be appreciated because I need this functionality to make my own portfolio. I already spend 3 days figuring out myself but I feel stuck at this point!

Thanks!

Zac Gordon

2 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey!

Check out this reference for how to name a custom post type archive page for displaying what you're looking to do: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display

Jahan Zeb
Jahan Zeb
11,169 Points

Thank you Zac,

So happy to see it work using taxonomy.php. I was doing everything right but was also defining 'rewrite' as a custom slug which was altering the permalink. I removed rewrite slug and it started working.

Thanks again for you help!