Tại sao nên chèn khung soạn thảo cho danh mục sản phẩm?
Trong thiết kế website bán hàng bằng Flatsome + WooCommerce, việc thêm nội dung mô tả đầu trang và cuối trang cho từng danh mục sản phẩm giúp:
Tối ưu SEO: Mỗi danh mục có thể được viết nội dung mô tả riêng, chứa từ khóa mục tiêu.
Tăng trải nghiệm người dùng: Khách hàng hiểu rõ hơn về nhóm sản phẩm họ đang xem.
Tùy biến linh hoạt: Chèn banner, video, HTML, shortcode… ở bất kỳ đâu trong danh mục.
Các bước chèn trình soạn thảo vào danh mục sản phẩm WooCommerce
Bước 1: Thêm đoạn mã vào functions.php theme con (child theme)
⚠️ Khuyến nghị sử dụng Flatsome Child Theme để tránh mất code khi cập nhật theme.
Dán đoạn code sau vào cuối file functions.php :
1 2 3 4 5 6 7 8 9 10 11 12 13 | add_action('init', function () { // Loại bỏ hook cũ (nếu có) remove_action('product_cat_edit_form_fields', 'top_text_taxonomy_edit_meta_field', 10); remove_action('product_tag_edit_form_fields', 'top_text_taxonomy_edit_meta_field', 10); remove_action('product_cat_edit_form_fields', 'bottom_text_taxonomy_edit_meta_field', 10); remove_action('product_tag_edit_form_fields', 'bottom_text_taxonomy_edit_meta_field', 10); // Thêm khung soạn thảo cho danh mục sản phẩm và tag add_action('product_cat_edit_form_fields', 'muathemewpgiare_editor_top', 10, 2); add_action('product_tag_edit_form_fields', 'muathemewpgiare_editor_top', 10, 2); add_action('product_cat_edit_form_fields', 'muathemewpgiare_editor_bottom', 10, 2); add_action('product_tag_edit_form_fields', 'muathemewpgiare_editor_bottom', 10, 2); }); |
Bước 2: Tạo hàm hiển thị trình soạn thảo nội dung
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | function muathemewpgiare_editor_top($term) { $meta = get_term_meta($term->term_id, 'cat_meta', true); $content = isset($meta['cat_header']) ? $meta['cat_header'] : ''; echo '<tr class="form-field"><th><label for="term_meta[cat_header]">Top Content</label></th><td>'; wp_editor($content, 'term_meta_cat_header', [ 'textarea_name' => 'term_meta[cat_header]', 'media_buttons' => true, 'textarea_rows' => 10 ]); echo '</td></tr>'; } function muathemewpgiare_editor_bottom($term) { $meta = get_term_meta($term->term_id, 'cat_meta', true); $content = isset($meta['cat_footer']) ? $meta['cat_footer'] : ''; echo '<tr class="form-field"><th><label for="term_meta[cat_footer]">Bottom Content</label></th><td>'; wp_editor($content, 'term_meta_cat_footer', [ 'textarea_name' => 'term_meta[cat_footer]', 'media_buttons' => true, 'textarea_rows' => 10 ]); echo '</td></tr>'; } |
Bước 3: Lưu dữ liệu khi chỉnh sửa danh mục
Thêm đoạn code sau để lưu nội dung trình soạn thảo khi bạn bấm “Cập nhật” trong trang chỉnh sửa danh mục:
1 2 3 4 5 6 7 8 9 10 11 12 13 | add_action('edited_product_cat', 'muathemewpgiare_save_term_editor'); add_action('edited_product_tag', 'muathemewpgiare_save_term_editor'); function muathemewpgiare_save_term_editor($term_id) { if (!isset($_POST['term_meta'])) return; $meta = get_term_meta($term_id, 'cat_meta', true); $meta = is_array($meta) ? $meta : []; $meta['cat_header'] = wp_kses_post($_POST['term_meta']['cat_header']); $meta['cat_footer'] = wp_kses_post($_POST['term_meta']['cat_footer']); update_term_meta($term_id, 'cat_meta', $meta); } |
Bước 4: Hiển thị nội dung trên giao diện danh mục sản phẩm
Chèn đoạn này vào file archive-product.php trong theme con (hoặc dùng hook trong Flatsome):
1 2 3 4 5 6 7 | $term = get_queried_object(); $meta = get_term_meta($term->term_id, 'cat_meta', true); if (!empty($meta['cat_header'])) echo wp_kses_post($meta['cat_header']); // ... danh sách sản phẩm ... if (!empty($meta['cat_footer'])) echo wp_kses_post($meta['cat_footer']); |
Lời kết
Với hướng dẫn trên từ muathemewpgiare, bạn đã có thể:
Tùy biến nội dung cho từng danh mục sản phẩm trên giao diện Flatsome.
Thêm trình soạn thảo đầy đủ tính năng như trong bài viết.
Hỗ trợ tối ưu SEO theo từng danh mục sản phẩm.
Đừng quên:
Mỗi danh mục có thể chèn thêm hình ảnh, video, shortcode giới thiệu.
Tận dụng các vị trí “Top Content” và “Bottom Content” để viết mô tả chuẩn SEO cho danh mục.
Nếu bạn gặp khó khăn trong việc cài đặt hoặc muốn phát triển thêm các tính năng khác cho theme Flatsome, hãy liên hệ đội ngũ muathemewpgiare.com để được hỗ trợ chuyên sâu hơn.
- Hướng dẫn code chức năng đăng xuất nhanh với SweetAlert2 trên WordPress
- Tạo văn bản wordpress Chuẩn SEO trong wordpress và hiệu ứng đẹp
- Cách seo lên top 1 google 2025 mà không phải ai cũng biết
- Tải Polylang Pro – Plugin Đa Ngôn Ngữ Chuyên Nghiệp Cho WordPress
- Chương Trình Flash Sale Giảm 50% Theme WordPress Cuối Năm 2024