I need template for my custom taxonomy term

I created a custom taxonomy slug is ‘event_type’ and created terms like social, educational and religious. I want to have template page where I can show posts related to these terms. you can say I need to access this url: http://localhost/test/event_type/social/

How I can create a template for this and for this url too http://localhost/test/event_type/

Read More

This taxonomy is related to custom post type named as ‘event’

Any help regarding this

Related posts

Leave a Reply

2 comments

  1. I used a modified version of Justin Tadlock’s code at his blog which allows you to create single post templates based on category (slug and id), author or tag.

    My modified code is below:

    /**
    * Filter the single_template with our custom function
    */
    add_filter('single_template', 'my_single_template');
    
    /**
    * Single template function which will choose our template
    */
     function my_single_template($single) {
        global $wp_query, $post;
    
        /**
        * Checks for single template by custom taxonomy 'event_types'
        * Change 'event_types' to whatever your custom taxonomy is called
        * Check by category slug and ID
        */
    
        //Only change 'event' post type
        if( 'event' != get_post_type($post) )
           return $single;
    
        //Where your single fiels are located inside your theme
        $single_template_path = get_stylesheet_directory().'/single/';
    
        //Get the taxonomy terms
        $cats = get_the_terms($post->ID,'event_types');
        if ( !$cats )
           return $single;
    
        foreach( $cats as $cat ) :
    
            if(file_exists($single_template_path . '/single-cat-' . $cat->slug . '.php'))
                return $single_template_path . '/single-cat-' . $cat->slug . '.php';
    
            elseif(file_exists($single_template_path . '/single-cat-' . $cat->term_id . '.php'))
                return $single_template_path  . '/single-cat-' . $cat->term_id . '.php';
    
        endforeach;
        return $single;
    
    }
    

    You would then add your new custom template files into /wp-content/themes/your-template-name/single, naming them accordingly.

    Using this function you could create a template for a tag called featured within a custom taxonomy of event_types by creating a file called single-cat-featured.php