Mặc định trong theme genesis chỉ cho phép chúng ta tìm kiếm nhưng nội dung bài viết liên quan mà không hề đả động đến những thứ khác như pages, new, article & videos… Vậy đối với trang web chuyên tập trung vào bài viết trên trang thì sao? sẽ không một kết quả nào được hiển thị lên cho doanh mục trang liên quan đến từ khóa như thế sẽ ảnh hưởng khá lớn đến nội dung và trải nghiệm của người đọc. Vậy trong bài viết này tôi sẽ hướng dẫn các bạn các tùy biến trang tìm kiếm trong genesis.
Chúng ta sẽ bắt đầu với các tập tin trong child theme, Bạn cần Tạo file search.php trong đó loại bỏ các Loop Genesis mặc định và bài viết vòng lặp tùy chỉnh của riêng bạn bao gồm các CPT tùy chỉnh bài viết sẽ hiện thị đầy đủ kết quả của bài viết, news, Article & Videos.
Và dưới đây là kết quả cuối cùng.
Hướng dẫn tùy biến trang kết quả tìm kiếm trong genesis framework
Chúng ta sẽ thực hiện việc này qua 3 bước bên dưới, lưu ý bạn có thể thay đổi một số thông tin cần thiết nếu có nhu cầu, sẽ có từng phần chú thích giúp bạn có thể hiểu và tùy chỉnh dễ dàng hơn.
Trước tiên tạo tệp tin có tên search.php trong thư mục chủ đề con có mã dưới đây.
<?php /** * Author: Hastimal Shah * Link: http://hastishah.com/ * Email: hasti@hastishah.com */ /* If you dont want to use any force layout then please comment line no 10 to 20 */ //* Force content-sidebar layout setting //add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_content_sidebar' ); // Remove // to uncomment the code //* Force sidebar-content layout setting //add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_sidebar_content' ); //* Force content-sidebar-sidebar layout setting //add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_content_sidebar_sidebar' ); //* Force sidebar-sidebar-content layout setting //add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_sidebar_sidebar_content' ); //* Force sidebar-content-sidebar layout setting //add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_sidebar_content_sidebar' ); //* Force full-width-content layout setting add_filter( 'genesis_pre_get_option_site_layout', '__genesis_return_full_width_content' ); /* Remove the default loop */ remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'hs_do_search_loop' ); /** * Outputs a custom loop * * @global mixed $paged current page number if paginated * @return void */ function hs_do_search_loop() { // create an array variable with specific post types in your desired order $post_types = array('news','article', 'page', 'post' ); // other custom post type can be added. echo '<div class="search-content">'; foreach ( $post_types as $post_type ) { // get the search term entered by user $s = isset( $_GET["s"] ) ? $_GET["s"] : ""; // accepts any wp_query args $args = (array( 's' => $s, 'post_type' => $post_type, 'posts_per_page' => 5, 'order' => 'ASC', 'orderby' => 'title' )); echo '<div class="post-type '. $post_type .'"><div class="post-type-heading">'. $post_type . 's</div>'; // remove post info remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); // remove post image (from theme settings) remove_action( 'genesis_entry_content', 'genesis_do_post_image', 8 ); // remove entry content // remove_action( 'genesis_entry_content', 'genesis_do_post_content' ); // remove post content nav remove_action( 'genesis_entry_content', 'genesis_do_post_content_nav', 12 ); remove_action( 'genesis_entry_content', 'genesis_do_post_permalink', 14 ); // force content limit add_filter( 'genesis_pre_get_option_content_archive_limit', 'hs_content_limit' ); // modify the Content Limit read more link add_filter( 'get_the_content_more_link', 'hs_read_more_link' ); // force excerpts // add_filter( 'genesis_pre_get_option_content_archive', 'hs_show_excerpts' ); // modify the Excerpt read more link add_filter( 'excerpt_more', 'hs_excerpt_read_more' ); // modify the length of post excerpts add_filter( 'excerpt_length', 'hs_excerpt_length' ); // remove entry footer remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_open', 5 ); remove_action( 'genesis_entry_footer', 'genesis_entry_footer_markup_close', 15 ); remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); // remove archive pagination remove_action( 'genesis_after_endwhile', 'genesis_posts_nav' ); // custom genesis loop with the above query parameters and hooks genesis_custom_loop( $args ); echo '</div>'; } echo '</div>'; // .search-content } function hs_content_limit() { return '150'; // number of characters } function hs_read_more_link() { return '... <a class="more-link" href="' . get_permalink() . '">Continue Reading</a>'; } function hs_show_excerpts() { return 'excerpts'; } function hs_excerpt_read_more( $more ) { return '... <a class="more-link" href="' . get_permalink() . '">Continue Reading</a>'; } function hs_excerpt_length( $length ) { return 30; // pull first 30 words } genesis();
Chú ý: Bạn tùy chỉnh các dòng từ 10 > 20 cho phụ hợp với bố cục chủ đề của bạn, tất cả các tùy chọn bố cục có sẵn, mẫu toàn chiều hiện tại được kích hoạt ở đây, chỉ cần bỏ chú thích mã bố trí mà bạn muốn sử dụng.
Hoặc bạn có thể tùy biến nhiều hơn với các tùy chọn bên dưới.
- Loại bỏ thông tin đăng – Dòng số 54
- Xóa bài đăng nổi bật – Dòng số 57
- Loại bỏ nội dung bài đăng – Dòng số 60
- Xoá nội dung đăng bài Nav – Dòng 63 & 64
- Sửa đổi giới hạn nội dung – Dòng số 67
- Sửa đổi giới hạn nội dung Đọc thêm Link – Dòng số 70
- Huỷ Nội dung Nội dung – Dòng số 73
- Sửa đổi giới hạn trích dẫn Đọc thêm Liên kết – Dòng số 76
- Loại bỏ thông tin đăng Meta – Dòng 82 đến 84
- Xóa Tập tin Lưu trữ – Dòng số 87
Lưu lại quay lại trang chủ và xem kết quả.