Bạn muốn thêm các trường tùy chỉnh cho danh mục category trong wordpress, thì bạn có thể dùng plugin ACF (Xem hướng dẫn thêm hình ảnh cho Category với Plugin ACF). Nhưng nếu bạn không muốn dùng plugin thì bạn có thể tham khảo bài hướng dẫn dưới đây:
Đầu tiên bạn thêm đoạn code dưới đây vào file functions.php (Hoặc để code gọn dễ nhìn thì bạn có thể tạo 1 file riêng trong folder functions với tên file custom-category-functions.php sau đó gọi trong file functions.php như sau: require get_template_directory() . ‘/functions/custom-category-functions.php’;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php //add extra fields to category edit form hook add_action ( 'edit_category_form_fields', 'extra_category_fields'); //add extra fields to category edit form callback function function extra_category_fields( $tag ) { //check for existing featured ID $t_id = $tag->term_id; $cat_meta = get_option( "category_$t_id"); ?> <tr class="form-field"> <th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Image Url'); ?></label></th> <td> <input type="text" name="Cat_meta[img]" id="Cat_meta[img]" size="3" style="width:60%;" value="<?php echo $cat_meta['img'] ? $cat_meta['img'] : ''; ?>"><br /> <span class="description"><?php _e('Image for category: use full url with '); ?></span> </td> </tr> <tr class="form-field"> <th scope="row" valign="top"><label for="extra1"><?php _e('extra field'); ?></label></th> <td> <input type="text" name="Cat_meta[extra1]" id="Cat_meta[extra1]" size="25" style="width:60%;" value="<?php echo $cat_meta['extra1'] ? $cat_meta['extra1'] : ''; ?>"><br /> <span class="description"><?php _e('extra field'); ?></span> </td> </tr> <tr class="form-field"> <th scope="row" valign="top"><label for="extra2"><?php _e('extra field'); ?></label></th> <td> <input type="text" name="Cat_meta[extra2]" id="Cat_meta[extra2]" size="25" style="width:60%;" value="<?php echo $cat_meta['extra2'] ? $cat_meta['extra2'] : ''; ?>"><br /> <span class="description"><?php _e('extra field'); ?></span> </td> </tr> <tr class="form-field"> <th scope="row" valign="top"><label for="extra3"><?php _e('extra field'); ?></label></th> <td> <textarea name="Cat_meta[extra3]" id="Cat_meta[extra3]" style="width:60%;"><?php echo $cat_meta['extra3'] ? $cat_meta['extra3'] : ''; ?></textarea><br /> <span class="description"><?php _e('extra field'); ?></span> </td> </tr> <?php } // save extra category extra fields hook add_action ( 'edited_category', 'save_extra_category_fileds'); // save extra category extra fields callback function function save_extra_category_fileds( $term_id ) { if ( isset( $_POST['Cat_meta'] ) ) { $t_id = $term_id; $cat_meta = get_option( "category_$t_id"); $cat_keys = array_keys($_POST['Cat_meta']); foreach ($cat_keys as $key){ if (isset($_POST['Cat_meta'][$key])){ $cat_meta[$key] = $_POST['Cat_meta'][$key]; } } //save the option array update_option( "category_$t_id", $cat_meta ); } } ?> |
Ở trên mình tạo thêm 4 trường. Nếu thành công thì trong trang quản lý category bạn sẽ thấy thêm 4 trường mới như sau:
Sau khi thêm thành công các trường tùy chỉnh cho danh mục để lấy các trường ra front-end bạn làm như sau:
– Gọi trong danh mục (Category.php) tham khảo code sau:
1 2 3 4 5 6 7 8 9 |
<?php //first get the current category ID $cat_id = get_query_var('cat'); //then i get the data from the database $cat_data = get_option("category_$cat_id"); //and then i just display my category image if it exists if (isset($cat_data['img'])){ echo '<div class="category_image"><img src="'.$cat_data['img'].'"></div>'; } |
– Gọi trong bài viết (single.php):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php //first get the current category ID $categories_current = get_the_category(); $cat_id = $categories_current[0]->cat_ID; // Lấy ID category của bài viết đang xem //then i get the data from the database $cat_data = get_option("category_$cat_id"); //and then i just display my category image if it exists if (isset($cat_data['img'])){ echo '<div class="category_image"><img src="'.$cat_data['img'].'"></div>'; } ?> |
Tham khảo: https://wordpress.stackexchange.com/questions/8736/add-custom-field-to-category