Tạo theme child WordPress để tùy chỉnh một theme WordPress một cách an toàn và hiệu quả, bạn nên sử dụng Child Theme. Việc sử dụng Child Theme cho phép bạn thay đổi và mở rộng chức năng của theme gốc mà không làm ảnh hưởng đến khả năng cập nhật của nó. Dưới đây là hướng dẫn chi tiết về cách tạo và sử dụng Child Theme trong WordPress.
Child Theme là một theme kế thừa tất cả các tính năng, kiểu dáng và template của một theme khác được gọi là Parent Theme. Bằng cách sử dụng Child Theme, bạn có thể tùy chỉnh và thêm mới các tính năng mà không làm thay đổi trực tiếp đến mã nguồn của Parent Theme. Điều này rất hữu ích vì khi Parent Theme được cập nhật, các tùy chỉnh của bạn sẽ không bị mất.
/* Theme Name: Twenty Twenty One Child Theme URI: http://example.com/twentytwentyone-child/ Description: Child theme cho Twenty Twenty One Author: Tên của bạn Author URI: http://example.com Template: twentytwentyone Version: 1.0.0 Text Domain: twentytwentyone-child */
Giải thích các dòng trên:
<?php function twentytwentyone_child_enqueue_styles() { $parent_style = 'twentytwentyone-style'; // Đây là handle của style từ Parent Theme wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' ); wp_enqueue_style( 'twentytwentyone-child-style', get_stylesheet_directory_uri() . '/style.css', array( $parent_style ), wp_get_theme()->get('Version') ); } add_action( 'wp_enqueue_scripts', 'twentytwentyone_child_enqueue_styles' ); ?>
Lưu ý:
Ví dụ: Ghi đè file header.php
Việc sử dụng Child Theme là một phương pháp tốt để tùy chỉnh WordPress một cách an toàn và linh hoạt. Nó giúp bạn duy trì các thay đổi tùy chỉnh mà không ảnh hưởng đến khả năng cập nhật và bảo mật của trang web.
Nếu bạn có bất kỳ thắc mắc hoặc cần hỗ trợ thêm, đừng ngần ngại hỏi!
Chúc bạn thành công trong việc tùy chỉnh theme WordPress của mình!
Để tùy chỉnh Custom Post Type và Custom Taxonomy trong WordPress, bạn có thể thực hiện thông qua code hoặc sử dụng plugin hỗ trợ. Dưới đây là cách bạn có thể tự tay tạo và tùy chỉnh các đối tượng này bằng code trong Child Theme của mình.
Custom Post Type cho phép bạn tạo ra các loại bài viết tùy chỉnh ngoài những loại mặc định như Post và Page. Ví dụ, bạn có thể tạo một loại bài viết mới như Portfolio, Event, Product, v.v.
Mở file functions.php trong Child Theme của bạn và thêm đoạn mã sau để tạo một Custom Post Type mới:
function custom_post_type_init() { $labels = array( 'name' => _x( 'Portfolios', 'post type general name' ), 'singular_name' => _x( 'Portfolio', 'post type singular name' ), 'menu_name' => _x( 'Portfolios', 'admin menu' ), 'name_admin_bar' => _x( 'Portfolio', 'add new on admin bar' ), 'add_new' => _x( 'Add New', 'portfolio' ), 'add_new_item' => __( 'Add New Portfolio' ), 'new_item' => __( 'New Portfolio' ), 'edit_item' => __( 'Edit Portfolio' ), 'view_item' => __( 'View Portfolio' ), 'all_items' => __( 'All Portfolios' ), 'search_items' => __( 'Search Portfolios' ), 'parent_item_colon' => __( 'Parent Portfolios:' ), 'not_found' => __( 'No portfolios found.' ), 'not_found_in_trash' => __( 'No portfolios found in Trash.' ) ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'portfolio' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), ); register_post_type( 'portfolio', $args ); } add_action( 'init', 'custom_post_type_init' );
Giải thích các tham số:
Sau khi thêm đoạn mã trên, lưu file functions.php. Đăng nhập vào bảng điều khiển WordPress của bạn và bạn sẽ thấy một mục mới tên là “Portfolios” trong menu quản trị. Bạn có thể bắt đầu tạo bài viết cho loại nội dung này.
Custom Taxonomy là cách để bạn tổ chức và phân loại nội dung một cách linh hoạt hơn so với các taxonomy mặc định như Category và Tag.
Tiếp tục mở file functions.php và thêm đoạn mã sau để tạo một Custom Taxonomy mới:
function create_custom_taxonomy() { $labels = array( 'name' => _x( 'Genres', 'taxonomy general name' ), 'singular_name' => _x( 'Genre', 'taxonomy singular name' ), 'search_items' => __( 'Search Genres' ), 'all_items' => __( 'All Genres' ), 'parent_item' => __( 'Parent Genre' ), 'parent_item_colon' => __( 'Parent Genre:' ), 'edit_item' => __( 'Edit Genre' ), 'update_item' => __( 'Update Genre' ), 'add_new_item' => __( 'Add New Genre' ), 'new_item_name' => __( 'New Genre Name' ), 'menu_name' => __( 'Genres' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'genre' ), ); register_taxonomy( 'genre', array( 'portfolio' ), $args ); } add_action( 'init', 'create_custom_taxonomy' );
Giải thích các tham số:
Sau khi lưu lại file functions.php, quay lại bảng điều khiển WordPress và mở bất kỳ bài viết nào thuộc loại Portfolio. Bạn sẽ thấy một mục mới trong giao diện chỉnh sửa bài viết cho phép bạn gán Genre cho bài viết đó.
Để hiển thị các Custom Post Type và Custom Taxonomy trên giao diện người dùng, bạn có thể tùy chỉnh file template trong Child Theme:
Nếu bạn cần thêm thông tin về cách tùy chỉnh giao diện hiển thị cho CPT và Taxonomy, hoặc gặp bất kỳ khó khăn nào, đừng ngần ngại hỏi thêm!
Để hiển thị danh sách bài viết từ một Custom Post Type (CPT) lên trang chủ của WordPress, bạn cần chỉnh sửa file template của trang chủ. Dưới đây là các bước thực hiện:
File template mặc định của trang chủ trong WordPress thường là index.php, home.php, hoặc front-page.php. Bạn có thể tạo hoặc chỉnh sửa file này trong Child Theme của bạn.
Bạn sẽ sử dụng lớp WP_Query để truy vấn các bài viết từ Custom Post Type và hiển thị chúng. Dưới đây là một ví dụ cơ bản:
Thêm đoạn mã sau vào vị trí mà bạn muốn hiển thị danh sách bài viết trên trang chủ:
<?php // Truy vấn bài viết từ Custom Post Type $args = array( 'post_type' => 'portfolio', // Thay 'portfolio' bằng tên CPT của bạn 'posts_per_page' => 10, // Số lượng bài viết muốn hiển thị ); $custom_query = new WP_Query( $args ); if ( $custom_query->have_posts() ) : ?> <div class="custom-post-list"> <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?> <div class="custom-post-item"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <?php if ( has_post_thumbnail() ) : ?> <div class="post-thumbnail"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?> </a> </div> <?php endif; ?> <div class="post-excerpt"> <?php the_excerpt(); ?> </div> </div> <?php endwhile; ?> </div> <?php // Phân trang nếu cần the_posts_pagination(); else : ?> <p>Không có bài viết nào được tìm thấy.</p> <?php endif; ?> <?php // Khôi phục lại truy vấn gốc wp_reset_postdata(); ?>
Giải thích mã:
Sau khi lưu file front-page.php hoặc index.php, truy cập trang chủ của website để kiểm tra xem danh sách bài viết từ Custom Post Type đã được hiển thị hay chưa.
Bạn có thể tùy chỉnh thêm về giao diện hiển thị danh sách bài viết bằng cách:
Nếu bạn cần trợ giúp thêm về việc tùy chỉnh giao diện hoặc gặp vấn đề nào, hãy cho mình biết!
Leave A Comment