Tutorial on custom term field in wordpress – Learn WordPress from a to z

Tutorials 0 lượt xem

In the previous post, I showed you how to custom taxonomy in wordpress , to continue the features related to taxonomy, today I will show you how to custom term field in wordpress.

So what is term field? How is its application? How do you create custom term fields? Today we are going to find out.

What is custom term field in wordpress?

By default in wordpress in each category (category) there will be fields such as: Name, Path, description, parent category … These fields are called term fields.

What is custom term field in wordpress?

By default, there are only this many fields in the wordpress category

And of course wordpress also provides the ability to create a new field here. When we go to create a new term field it is called custom term field in wordpress.

Custom term field will apply to all taxonomy, not just category.

Create custom term field in wordpress

For example: I want to custom 1 field to contain the image link representing the category, corresponding to each category there will be 1 link.

Please insert the following code into the functions.php file of the theme you are using.

Create a custom term field in the new add:

<?php function add_feature_group_field($taxonomy) { ?><div class="form-field term-group">
       <label for="location"><?php _e('Ảnh đại diện'); ?></label>
       <input class="controls" type="text" id="linkcat" name="linkcat">
</div> 
<?php }
add_action( 'category_add_form_fields', 'add_feature_group_field', 10, 1);
function save_category( $term_id, $tt_id ){
    if( isset( $_POST&#91;'linkcat'&#93; ) && '' !== $_POST&#91;'linkcat'&#93; ){
        add_term_meta( $term_id, 'linkcat', $_POST&#91;'linkcat'&#93;, true );
    }
}
add_action( 'created_category', 'save_category', 10, 2 );
?><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>

Explain:

  • The  add_feature_group_field function is used to display the link input area, this function will be hooked into the  category_add_form_fields action,  Note the word  category is the name of the taxonomy you need to custom.
  • The  save_category  function is responsible for checking the data and saving it to the database, this function will be hooked into the  create_category action,  Note the word  category  is the name of the taxonomy that needs custom.

Results: 

Added field Avatar

Display in category edit area:

The above code only displays the area to add a new category, and if you want to display it in the category editing area , use the following code.

<?php function edit_feature_group_field($taxonomy) { 
    $term_id = $taxonomy->term_id; 
    $linkcat= get_term_meta( $term_id, $key = 'linkcat', true ); 
?&gt;
<tr class="form-field">
<th valign="top" scope="row">
        <label for="bedroom">Ảnh đại diện</label>
    </th>
    <td>
        <input class="controls" name="linkcat" type="text" id="linkcat" value="&lt;?php echo $linkcat; ?&gt;">
</td>
</tr><?php }
add_action( 'category_edit_form_fields', 'edit_feature_group_field', 10, 1);
function update_category( $term_id, $tt_id ){
    if( isset( $_POST&#91;'linkcat'&#93; ) && '' !== $_POST&#91;'linkcat'&#93; ){
         update_term_meta( $term_id, 'linkcat', $_POST&#91;'linkcat'&#93; );
    }
} 
add_action( 'edited_category', 'update_category', 10, 2 );
?><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>
</div>

Explain: 

  • $linkcat = get_term_meta( $term_id, $key = ‘linkcat’, true ); is the code to get the value of the current link.
  • The  edit_feature_group_field  function to display the link input in the category editing area. This function is hooked to the action  category_edit_form_fields,  Note that the word  category is the name of the taxonomy
  • The  update_category  function updates the data to the database, this function is hooked into the edited_ category action,  Note that  category is the name of the taxonomy

Create custom term field with plugin

Like all other functions in wordpress, this custom term field also has dozens of plugins to support. The plugin will be very professional and the fields are also diverse.

If you need to, you can use some of the following plugins:

I have shared acf pro plugin here, if you need it, download it:

Get custom term field to front end

To get a term field to the front end we use the following function:

get_term_meta( $term_id, $key, true )

For example:

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

Summary:

So today I show you how to custom term field in WordPress . This feature is applied a lot, for example, creating an avatar for a category…

Hope this little knowledge will help you learn wordpress theme programming well

Hello everyone, see you in the next post

Bài viết liên quan