Hi guys! Continuing with the topic of wordpress theme programming , today I will show you how to custom taxonomy in wordpress.
So what is a taxonomy? Is it important or not? Why do we need to go find it? … All I will clarify in this post.
Contents
What is custom taxonomy in wordpress?
By default, when installing wordpress, in the post section (Articles) we will see 2 sections: Categories and tags . These 2 sections in wordpress are called taxonomy (Category taxonomy).
Creating a new category taxonomy with a structure similar to a category or tag is called a custom taxonomy in wordpress.
For example: We have a custom post type that is a product, now want to divide the product by category, by default wordpress does not provide it. We have to create a custom taxonomy “category” to make the product category…

Create custom taxonomy in wordpress
To create a custom taxonomy, insert the following code into the functions.php file of the theme you are using.
Well, I will create a custom taxomony as the location in the post .
Syntax
function location_custom_taxonomy() {
$labels = array(
'name' => 'Địa điểm',
'singular' => 'Địa điểm',
'menu_name' => 'Địa điểm'
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy('dia-diem', 'post', $args);
}
add_action( 'init', 'location_custom_taxonomy', 0 );
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span>
</div>
Attention
- The hierarchical parameter can take 2 values, if true , the function is similar to the category , and false , the function is similar to the tag .
- The register_taxonomy function takes 3 parameters
- ‘dia-diem’ This is the taxonomy’s slug that will later be used to get data. Name as you like, write immediately without accents
- ‘post’ this is the post type that displays this taxonomy. For example, if you want to create a taxonomy for a product, this place must be ‘san-pham’
- $args is the parameter declared above
Create custom taxonomy with plugin
In addition to creating a taxonomy with the code above, there are also many plugins that support this. I also had an article about creating custom taxonomy with the toolset types plugin you can refer to. Or check out some of the following free plugins
Get custom taxonomy in wordpress
Get a list of custom taxonomy
Similar to get category in wordpress , we use the following code to insert it into the position to display in the theme
<?php $args = array(
'hide_empty' => 0,
'taxonomy' => 'danh-muc',
);
$cates = get_categories( $args );
foreach ( $cates as $cate ) { ?>
Note: The parameter taoxonomy receives the value of the slug of the taxonomy that we declare when creating it.
Get post with custom taxonomy in wordpress
To get a list of articles belonging to a certain taxonomy, by default we will name the file: taxonomy-{slug}.php as in the example above, we will name the file taxonomy-dia-diem.php
For new wp_query we will have the following get post code:
<?php $args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'dia-diem' => 'slug'
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
// Thông tin cần lấy của 1 bài viết
<?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span>
</div>
Note: the slug in the above parameter is the taxonomy’s slug to get.
summary
So today I showed you how to custom taxonomy in wordpress. This is also one of the very important knowledge. Make our website more professional.
I will continue to update the latest articles in this topic. Please keep an eye on it!
Thank you and hello everyone.

