Custom Taxonomy for Custom Post Types

Taxonomies are a great way to group things together and help us to search posts belonging to a specific group. In WordPress we generally use Categories and Tags as taxonomies. The steps given below explain how to create custom taxonomies for your CPT. 

Step 1 : Register Custom Post Type

Open functions.php file in your theme and Paste these Code, in our case we will register Photogallery as CPT. 

// Photo Gallary Customs Post Register
function create_posttype()
{
    register_post_type(
        'photogallery',
        array(
            'labels' => array(
                'name' => __('Photo gallery'),
                'singular_name' => __('photogallery')
            ),
            'public' => true,
            'menu_position'       => 5,
            'supports'            => array('editor', 'title', 'thumbnail',),
            'has_archive' => true,
            'rewrite' => array('slug' => 'photogallery'),
            'taxonomies'  => array('photogallery', 'photogallery-category'),
        )
    );
}
add_action('init', 'create_posttype');

Making Custom Archive For Custom Post Type


Create a file name archive-photogallery.php in theme's root directory and paste these code in it. 

Url: mywebsite.com/photogallery


Note: (if url does't work please do that)This will re-write the htaccess file and then the re-write should work.
  1. Navigate to Settings -> permalinks
  2. Change the permalink structure to Default
  3. Save settings
  4. Change to custom structure or post name (or any other structure)
  5. Save Settings

<!-- archive-photogallery.php -->

<?php get_header(); ?>
<div id="primary">
    <div id="content" role="main">
        <?php $mypost = array('post_type' => 'photogallery',);
        $loop = new WP_Query($mypost); ?>
        <?php while ($loop->have_posts()) : $loop->the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <header class="entry-header">
                    <div style="float: top; margin: 10px">
                        <?php the_post_thumbnail(); ?>
                    </div>
                    <strong>Title: </strong><?php the_title(); ?><br />
                    <br />
                </header>
                <div class="entry-content">
                    <!-- <?php the_content(); ?> -->
                </div>
                <hr />
            </article>
        <?php endwhile;  ?>
    </div>
</div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>

Step 2 : Implementation of Custom Function and Registering Custom Taxonomy


Paste these Code in functions.php as well, in that case we will register Photogallery's  custom Taxonomy. 


//Create category for Photo post type
function tr_create_my_taxonomy()
{
    register_taxonomy(
        'photogallery-categories',
        'photogallery',
        array(
            'label' => __('Photogallery Categories'),
            'rewrite' => array('slug' => 'photogallery-category'),
            'hierarchical' => true,
            'has_archive' => true
        )
    );
}
add_action('init', 'tr_create_my_taxonomy');

Making Custom Archive For Custom Taxonomy


Create a file name taxonomy-photogallery-categories.php in theme's root directory and paste these code in it. 

<?php get_header(); ?>
<div class="custom_container">
    <div class="photo_subcat_posts_grid">
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                <div class="single-block auto">
                    <div class="img-box">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php if (has_post_thumbnail()) {
                                the_post_thumbnail('custom-size');
                            } else { ?>
                                <img src="<?php bloginfo('template_directory'); ?>/img/default-img_final.gif" alt="<?php the_title(); ?>" />
                            <?php } ?></a>
                    </div>
                    <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php echo wp_trim_words(get_the_title(), 10); ?></a></h4>
                </div>
        <?php endwhile;
        endif ?>
    </div>
    <div class="pagination1">
        <div class="pagi_inner">
            <?php echo paginate_links(array(
                'prev_text' => __('&laquo;', 'textdomain'),
                'next_text' => __('&raquo;', 'textdomain'),

            )); ?>
        </div>
    </div>
</div>
<?php get_footer(); ?>

Other Support Options:


To support custom post type post in different templaet

// To Support Custom Post Format in different Template

function namespace_add_custom_types($query)
{
    if (is_single() || is_category() || is_tag() && empty($query->query_vars['suppress_filters'])) {
        $query->set('post_type', array(
            'post', 'nav_menu_item', 'photogallery','videogallery'
        ));
        return $query;
    }
}
add_filter('pre_get_posts', 'namespace_add_custom_types');

Custom Post type Post Query



<?php $args1 = array(
    'post_type' => 'photogallery',
    'posts_per_page' => 10,
    'order' => 'DESC',
);
$lastpost = new WP_Query($args1);
if ($lastpost->have_posts()) : while ($lastpost->have_posts()) : $lastpost->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                <?php if (has_post_thumbnail()) {
                    the_post_thumbnail();
                } else { ?>
                    <img src="<?php bloginfo('template_directory'); ?>/img/default-img_final.gif" alt="<?php the_title(); ?>" />
                <?php } ?></a>
            <a href="<?php the_permalink(); ?>"><span class="media-body"><?php echo wp_trim_words(get_the_title(), 10); ?></span></a>
        </li>
<?php endwhile;
endif ?>