Instructions to get category in wordpress from a to z

Tutorials 0 lượt xem

Continuing with the topic of wordpress theme programming , this article will guide you to get category in wordpress as well as get taxomony (A custom form of category). This article will help you display the list of categories as well as product categories… on the front end.

In the website I also use this code to make the list of categories as shown below.

Instructions to get category in wordpress from a to z

Syntax get category in wordpress

We will use the get_categories() function to do the above

<?php $args = array(
    'type'      => 'post',
    'number'    =&gt; 10,
    'parent'    =&gt; 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ) { ?&gt;
     <?php echo $category->name ; ?&gt;
<?php } ?><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>
</div>

The get_categories() function takes an array ($args) as an argument.

The return result of the function get_categories() is an array (array) in which there are not many objects, each corresponding object is a category and the category will not have all the elements of that category.

Parameters of get category function in wordpress

As I said above, the parameter of the get_categories() function is an array, the composition of that array depends on the user’s requirements and is passed in accordingly. As follows:

<?php $args = array(
	'type'                     => 'post',
	'child_of'                 =&gt; 0,
	'parent'                   =&gt; '',
	'orderby'                  =&gt; 'name',
	'order'                    =&gt; 'ASC',
	'hide_empty'               =&gt; 1,
	'hierarchical'             =&gt; 1,
	'exclude'                  =&gt; '',
	'include'                  =&gt; '',
	'number'                   =&gt; '',
	'taxonomy'                 =&gt; 'category',
	'pad_counts'               =&gt; false 
); 
?&gt;
<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>
</div>

Depending on the requirements, we add or remove the above parameters.

In there: 

  • ‘type’  is the post type when initially installing wordpress, there will be 2 types:  ‘post’ and ‘page’ . Otherwise, if this condition is not used, it will receive the value  ‘post’.
  • ‘child_of’  will take 2 values ​​of  1 or 0,  If it takes value  1  , it will only take  categories with subcategories . If it takes the value 0, it will take  all the categories . By default it takes the value  0.
  • ‘parent’  Will receive the value of the  parent id,  When entering the parent id here it will list all the child categories of the parent.
  • ‘orderby’  This value it thinks is sorted by, the received value can be:  id, name, slug, count, term_group . By default it will take the value of  name .
  • ‘order’  Will take 2 values:  ASC  or  DESC  means to sort in descending or ascending order. By default it is ASC.
  • ‘hide_empty’   Will take 2 values ​​as  1 or 0,  If it takes value 1, it will  not  display categories without posts.
  • hierarchical ‘ Whether to display the directory in tree form or not. If yes enter value 1, otherwise value 0
  • ‘exclude’  The value received here is an array of ids that do not want to be displayed in the category.
  • ‘include’  The received value is an array of ids that will appear in the category
  • ‘number’  The number of categories you want to display.
  • ‘taxomony’ slug of the required taxomony applies to the custom category

Return values ​​in object:

$category-&gt;term_id // lấy id của chuyên mục
$category-&gt;name // lấy tên của chuyên mục
$category-&gt;slug // lấy slug của chuyên mục
$category-&gt;term_group // lấy nhóm
$category-&gt;term_taxonomy_id // id của chuyên mục
$category-&gt;taxonomy // lấy slug
$category-&gt;description // lấy mô tả chuyên mục
$category-&gt;parent // lấy id chuyên mục cha
$category-&gt;count // lấy số lượng bài viết của chuyên mục
$category-&gt;cat_ID // lấy id của category
$category-&gt;category_count // lấy số lượng bài viết của category
$category-&gt;category_description // lấy mô tả của category
$category-&gt;cat_name // lấy tên của category
$category-&gt;category_nicename // lấy slug của category
$category-&gt;category_parent // lấy id cha của category
<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>
</div>

We use the following function to spread the link of the category as follows:

<?php echo get_term_link($category->slug, 'category'); ?&gt;<div><span>1</span></div>

The category place is the slug of the taxomony to get, for example, getting the link of the woo product category will be ” product_cat

Get category when id, slug or name is known in advance

When you know the id of a category you want to get all the information of that category, then you use the get_term_by  function specifically to get the following:

// get category khi biết trước id
$category = get_term_by('id', 10, 'category')

// get category khi biết trước name
$category = get_term_by('name', 'Tin tức', 'category')

// get category khi biết trước slug
$category = get_term_by('slug', 'tin-tuc', 'category')
<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>
</div>

The return value of this function is also an object, the elements in the object are similar to the above

Get category in wordpress dropdown select

To get category in wordpress in dropdown form we use the specific wp_dropdown_categories function as follows:

<?php $args = array(
	'show_option_all'    => '',
	'show_option_none' 	 =&gt; __( 'Danh mục' ),
	'option_none_value'  =&gt; '',
	'orderby'            =&gt; 'ID',
	'order'              =&gt; 'ASC',
	'show_count'         =&gt; 0,
	'hide_empty'         =&gt; 0,
	'child_of'           =&gt; 0,
	'include'            =&gt; '',
	'echo'               =&gt; 1,
	'selected'           =&gt; 0,
	'hierarchical'       =&gt; 1,
	'name'               =&gt; 'category',
	'id'                 =&gt; 'category',
	'class'              =&gt; 'form-control',
	'depth'              =&gt; 0,
	'tab_index'          =&gt; 0,
	'taxonomy'           =&gt; 'category',
	'hide_if_empty'      =&gt; false,
	'value_field'	     =&gt; 'slug',
); ?&gt;
<?php wp_dropdown_categories( $args ); ?><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><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span>
</div>

I have applied this code to make the product search function by category , you can try it.

summary

Today I have introduced you how to get category in wordpress, hope this little knowledge will help you in the process of learning wordpress theme programming.

Hello friends.

Bài viết liên quan