• mcography

    (@mcography)


    Hey all, I’m having some trouble figuring out how to display custom taxonomies. I’ve created a custom post type called ‘work’ and a custom taxonomy for ‘work’ called ‘project-type.’ Here’s the code I used to create the taxonomy:

    add_action( 'init', 'create_my_taxonomies', 0 );
    	function create_my_taxonomies() {
    	    register_taxonomy(
    	        'work_project_type',
    	        'work',
    	        array(
    	            'labels' => array(
    	                'name' => 'Project Types',
    	                'add_new_item' => 'Add New Project Type',
    	                'new_item_name' => "New Project Type Name"
    	            ),
    	            'rewrite' => array( 'slug' => 'project-type' ),
    	            'show_ui' => true,
    	            'show_tagcloud' => false,
    	            'hierarchical' => true,
    	            'has_archive' => true
    	        )
    	    );
    	}

    In the WordPress admin dashboard, I created several project types (ex. video, photography, web design, etc.). Here’s my question: how do I display a list of all the items in each project type? For example, I’d like to be able to click ‘photography’ and see a list of all the posts in the ‘photography’ category. Currently, when I go to https://mydomain.com/project-type/photography/ it displays all of the posts in the custom post type ‘work’ rather than just those in ‘photography.’

    From looking at the WordPress Template Hierarchy, it looks like I should create a file called template-project-type.php, but I’ve tried that and it isn’t working. I also tried template.php, template-project_type.php, template-work_project_type.php but none of them are working. Help?

    (P.S.- Here’s my site.)

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter mcography

    (@mcography)

    The problem appears to be that it is calling archive-work.php rather than any of the taxonomy templates I created. That doesn’t make sense to me, because according to the template hierarchy, it should look for the taxonomy templates first. I’m confused.

    Thread Starter mcography

    (@mcography)

    If it helps, I also have this code in my CPT plugin file.

    add_filter( 'template_include', 'include_template_function', 1 );
    	function include_template_function( $template_path ) {
    	    if ( get_post_type() == 'work' ) {
    	        if ( is_single() ) {
    	            // checks if the file exists in the theme first,
    	            // otherwise serve the file from the plugin
    	            if ( $theme_file = locate_template( array ( 'page-work.php' ) ) ) {
    	                $template_path = $theme_file;
    	            } else {
    	                $template_path = plugin_dir_path( __FILE__ ) . '/page-work.php';
    	            }
    	        }
            elseif ( is_archive() ) {
                if ( $theme_file = locate_template( array ( 'archive-work.php' ) ) ) {
                    $template_path = $theme_file;
                } else { $template_path = plugin_dir_path( __FILE__ ) . '/archive-work.php';
                }
            }
            }
    	    return $template_path;
    	}

    Is there a way to edit it so that it doesn’t default to archive-work.php for the project types? I’m new to CPTs and custom taxonomies, so I appreciate any help you can provide. Thanks.

    Were you able to resolve this issue?

    Did you find the answer yet??
    I also created a custom post type an associated custom taxonomy.
    The post type archive page just uses archives.php but I don’t know how to show the custom taxonomy archives page?

    In my situation, the issue was that I was thinking of the taxonomy as a custom post type and tried to use archive-$posttype.php for it. Silly mistake.

    You should try to use either taxonomy-$taxonomy-$term.php or just taxonomy-$taxonomy.php

    So if you had magazines as a CPT and schedule as the taxonomy and daily,weekly,monthly, quarterly as terms you would do
    taxonomy-schedule.php or to have a very specific appearance taxonomy-schedule-quarterly.php

    @brian, did you do this at plugin-level? I have a plugin in which I’ve created a custom taxonomy and when viewing the category page, it uses the themes archive template. I’m trying to create a custom template for viewing these new categories but within the plugin itself instead of creating the file in the theme folder.

    I’ve created a folder within my plugin /templates and a file: taxonomy-$taxonomy.php but I’m pretty sure I’m missing something that hooks it up inside WordPress, because the theme archive.php is still being used.

    Do you have any suggestions?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Taxonomy Template/Archive’ is closed to new replies.