Phần mô tả mặc định của danh mục sản phẩm khá là đơn giản và thô sơ, cũng không có sẵn trình soạn thảo WYSIWYG để dễ dàng thêm các nội dung vào. Vì vậy trong bài viết này mình sẽ hướng dẫn bạn thêm 1 trường mô tả vào phần danh mục sản phẩm với đầy đủ tool nhé.
Code thêm tool vào danh mục sản phẩm
Đầu tiền chỉ cần copy code sau và thêm vào file functions.php của child theme bạn đang xài là được.
|
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 |
function replace_product_cat_desc_editor( $term ) { ?> <script> jQuery(document).ready(function($) { $('#tag-description').closest('.form-field').hide(); }); </script> <?php // Lấy nội dung mô tả hiện tại $description = ( isset( $term->description ) ) ? $term->description : ''; ?> <tr class="form-field term-description-wrap"> <th scope="row"><label for="description">Mô tả</label></th> <td> <?php // Hiển thị trình soạn thảo WYSIWYG wp_editor( htmlspecialchars_decode( $description ), 'tag_description', array( 'textarea_name' => 'description', 'media_buttons' => true, 'textarea_rows' => 10, 'teeny' => false, 'quicktags' => true, 'editor_class' => 'full-text-editor', ) ); ?> <p class="description">Phần mô tả chi tiết hiển thị trên trang danh mục sản phẩm.</p> </td> </tr> <?php } // Dùng cho trang chỉnh sửa chuyên mục sản phẩm add_action( 'product_cat_edit_form_fields', 'replace_product_cat_desc_editor', 10, 1 ); remove_filter( 'pre_term_description', 'wp_filter_kses' ); remove_filter( 'term_description', 'wp_kses_data' ); function save_product_cat_desc_editor( $term_id ) { if ( isset( $_POST['description'] ) ) { $data = array( 'description' => wp_kses_post( $_POST['description'] ) ); wp_update_term( $term_id, 'product_cat', $data ); } } add_action( 'edited_product_cat', 'save_product_cat_desc_editor' ); add_action( 'create_product_cat', 'save_product_cat_desc_editor' ); |
Sau khi thêm vào là xong nhé, bình thường nội dung mô tả danh mục sản phẩm sẽ nằm bên trên danh sách sản phẩm trong trang danh mục nhé.
Code thêm tool vào danh mục bài viết
Copy code sau và thêm vào file 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 |
// Bật WYSIWYG editor cho mô tả danh mục bài viết (Category) function category_desc_wysiwyg_editor( $term ) { ?> <script> jQuery(document).ready(function($) { $('#tag-description').closest('.form-field').hide(); }); </script> <?php $description = isset( $term->description ) ? $term->description : ''; ?> <tr class="form-field term-description-wrap"> <th scope="row"><label for="description">Mô tả</label></th> <td> <?php wp_editor( htmlspecialchars_decode( $description ), 'tag_description', array( 'textarea_name' => 'description', 'media_buttons' => true, 'textarea_rows' => 10, 'teeny' => false, 'quicktags' => true, ) ); ?> <p class="description">Mô tả hiển thị trên trang danh mục bài viết.</p> </td> </tr> <?php } add_action( 'category_edit_form_fields', 'category_desc_wysiwyg_editor', 10, 1 ); // Cho phép HTML trong mô tả danh mục remove_filter( 'pre_term_description', 'wp_filter_kses' ); remove_filter( 'term_description', 'wp_kses_data' ); // Lưu nội dung editor function save_category_desc_wysiwyg_editor( $term_id ) { if ( isset( $_POST['description'] ) ) { wp_update_term( $term_id, 'category', array( 'description' => wp_kses_post( $_POST['description'] ) ) ); } } add_action( 'edited_category', 'save_category_desc_wysiwyg_editor' ); add_action( 'create_category', 'save_category_desc_wysiwyg_editor' ); |











ad có thể lên code áp dụng Với mô tả danh mục không?
cảm ơn.
Okie để mình bổ sung cho hen
Mình đã bổ sung nhé
Ad có thể lên code hướng dẫn với mô tả danh mục bài viết không?
Cảm ơn.