Theme wordpress du lịch 21. Trong theme này sẽ có 1 phần làm nó hơi chậm nên các bạn hãy thay bằng phần này trong funtion.php nhé.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | if (function_exists('acf_add_options_page')) { // Tạo options page chỉ khi cần thiết if (!get_option('theme_options_page_created')) { acf_add_options_page(array( 'page_title' => 'Cài đặt themes', 'menu_title' => 'Cài đặt themes', 'menu_slug' => 'theme-general-settings', 'capability' => 'edit_posts', 'redirect' => false, 'position' => 3 )); update_option('theme_options_page_created', true); } } class ws247_widget extends WP_Widget { public function __construct() { parent::__construct( 'ws247_widget', 'Sản phẩm liên quan', array('description' => 'Widget hiển thị Widget') ); } public function form($instance) { $default = array(); $instance = wp_parse_args((array) $instance, $default); } public function update($new_instance, $old_instance) { $instance = $old_instance; return $instance; } public function widget($args, $instance) { extract($args); $title = get_field('title', 'widget_' . $args['widget_id']); $number = get_field('pos', 'widget_' . $args['widget_id']); if ($title) : // Kiểm tra xem đã có widget chưa, tránh thực hiện truy vấn lặp lại $cache_key = 'ws247_widget_' . $args['widget_id']; $cached_widget = get_transient($cache_key); // Kiểm tra xem có dữ liệu cached không if (!$cached_widget) { ob_start(); // Bắt đầu ghi dữ liệu ra bộ đệm ?> <aside class="widget" id="fixed-widget-custom"> <span class="widget-title"><?php echo $title ?></span> <div class="is-divider small"></div> <?php global $post; // Lấy taxonomy terms của post $customTaxonomyTerms = wp_get_object_terms($post->ID, 'product_cat', array('fields' => 'ids')); // Truy vấn sản phẩm liên quan $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $number, 'tax_query' => array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $customTaxonomyTerms ) ), 'post__not_in' => array($post->ID), ); // Truy vấn các sản phẩm liên quan $relatedPosts = new WP_Query($args); if ($relatedPosts->have_posts()) { ?> <div class="related-tour"> <?php while ($relatedPosts->have_posts()) { $relatedPosts->the_post(); global $product; ?> <a href="<?php the_permalink(); ?>"> <div class="related-item"> <div class="box-image"> <?php the_post_thumbnail('full'); ?> </div> <div class="content"> <div class="title"> <?php the_title(); ?> </div> <div class="icon"> <img src="<?php echo get_stylesheet_directory_uri() . '/assets/images/map.svg'; ?>" alt="Location"> <span><?php the_field('place'); ?></span> </div> <div class="icon price"> <img src="<?php echo get_stylesheet_directory_uri() . '/assets/images/money.svg'; ?>" alt="Price"> <span><?php echo $product->get_price_html(); ?></span> </div> </div> </div> </a> <?php } ?> </div> <?php } wp_reset_postdata(); ?> </aside> <?php $cached_widget = ob_get_clean(); // Lấy dữ liệu từ bộ đệm và lưu vào biến set_transient($cache_key, $cached_widget, 12 * HOUR_IN_SECONDS); // Cache dữ liệu trong 12 giờ } // Hiển thị widget từ bộ nhớ cache echo $cached_widget; endif; } } // Đăng ký widget add_action('widgets_init', function() { register_widget('ws247_widget'); }); |
Đánh giá
Chưa có đánh giá nào.