WordPress.org

Ready to get started?Download WordPress

Forums

Taxonomy template file in custom theme (13 posts)

  1. rickhuby
    Member
    Posted 3 years ago #

    Hi,

    I am trying to build a WP website with a custom theme which I am trying to code from scratch.

    Most of it is coming together fine but I am really stumped on taxonomy templates.

    I have create a custom post type of products and a custom taxonomy related to products called product-type. Now I am trying to get a list of all the products in a particular product-type which I believe should mean creating a taxonomy.php file in the theme and doing all the code in there, however whenever I visit the taxonomy page I just get the default index.php page.

    I have used the following code to pull out the URL's for the taxonomy terms so the URL's should be correct.

    $product_types = get_terms('product_type', 'orderby=count&hide_empty=0');
    
    	foreach($product_types as $product_type) {
    		echo '<p>' .$product_type->name. ' - ' .$product_type->slug. '</p>';
    	}

    In my functions file I have create the products custom post type and the product_type custom taxonomy using the following code:

    register_taxonomy(
    		'product_type',
    		'products',
    		array(
    			'label' => __('Product Type'),
    			'sort' => true,
    			'rewrite' => array( 'slug' => 'product-type', 'with_front' => false )
    		)
    register_post_type(
    	'products',
    	array(
    		'labels' => array (
    			'name' => __('Products'),
    			'singular_name' => __('Product')
    		),
    		'public' => true,
    		'supports' => array(
    			'title',
    			'editor',
    			'excerpt',
    			'thumbnail',
    			'custom-fields'
    		),
    		'taxonomies' => array(
    			'product_type'
    		)
    	)
    );
  2. rickhuby
    Member
    Posted 3 years ago #

    I was wondering if it was something to do with the 'with_front' setting but I've not been able to find anything to do with that which has made anything work.

  3. rickhuby
    Member
    Posted 3 years ago #

    No one got an idea on this one?

  4. kakubei
    Member
    Posted 3 years ago #

    We're having the exact same error and I cannot find any info out there for this. Does anyone have any ideas?

  5. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    dunno if this helps or not, but it's not just a taxonomy.php you make.

    I have a custom tax called product_category associated with a CPT

    so I made a file called taxonomy-product_category.php which works perfectly

    didn't need to do any custom query or anything.....

  6. kakubei
    Member
    Posted 3 years ago #

    I've tried this but it didn't work either. What is a CPT? Sorry, I'm kinda new to WordPress.

    We have a custom tax called "tax_course" so I created a "taxonomy-tax_course.php" template and it doesn't work, goes straight to index.php page.

    According to the WordPress documentation, if it doesn't find that taxonomy-[custom_tax_name].php page it goes on to the taxonomy.php page.

    None of this is working for us and we can't figure out why.

  7. Steveorevo
    Member
    Posted 3 years ago #

    Yeah, I can confirm that I couldn't get this to work either. taxonomy-(insert your post_type here).php doesn't work. One thing I could do is force a query in a page template or before any instance of the loop. Just before the posts loop do

    <?php query_posts("post_type=somenewtype"); ?>

    Where somenewtype is the new kind of post_type taxonomy you wish to display. If you want to do a taxonomy tag-like query, can do

    <?php query_posts("post_type=somenewtype&my_registered_tax=somevalue"); ?>

  8. Abigail
    Member
    Posted 2 years ago #

    Yeah, I can confirm that I couldn't get this to work either. taxonomy-(insert your post_type here).php doesn't work.

    It actually works. It's taxonomy-(insert your taxonomy here).php that works.

  9. Steveorevo
    Member
    Posted 2 years ago #

    Thanks for the reply Abingabanger. Has anyone come across an example post of this working with a custom taxonomy page? I ended up having to use a page template and reworking the query before the loop. Using a taxonomy-xxx.php would have been much cleaner. I've found many sites on implementing the taxonomy but actually displaying the contents via the theme hierarchy has left me at a loss :-(

  10. winningsem
    Member
    Posted 2 years ago #

    Well I have lots of trial and I couldnt get it to work with the taxonomy-taxonomy-(insert your taxonomy here).php
    then I tried to use

    single-(insert your taxonomy here).php

    ans surprisingly it worked :)

    So I guess the

    taxonomy-(insert your taxonomy here).php

    is equivalent to category.php

    and this
    single-(insert your taxonomy here).php

    is equivlant to single.php just the wordpress docs didn't explain that good enough

  11. Steveorevo
    Member
    Posted 2 years ago #

    Has anyone tried this with custom permalinks? An associate mentioned he did get it to work but only with the default permalink turned on.

  12. winningsem
    Member
    Posted 2 years ago #

    yeah using the way i mentioned above worked with custom permalinks

  13. JHouse
    Member
    Posted 2 years ago #

    Rev. Voodoo's solution worked like a champ. Stoked!!!

Topic Closed

This topic has been closed to new replies.

About this Topic