Mai Việt Sơn

Loading...

Vikinger

Next:

0

[Woocommere] Hướng dẫn lấy ảnh đại diện sản phẩm mới nhất làm ảnh của danh mục

1124

Post Engagements

(Lượt xem)

6,76%

95

Post Like

All

10,00%

Ngày đăng: 2 years trước -

1124

95

Chức năng

Mình hướng dẫn các bạn chứ năng lấy ảnh đại diện của sản phẩm mới nhất để làm ảnh đại diện cho danh mục sản phẩm đó luôn

  • Dán code vào function chạy ngay
  • Lưu ý để chạy lần đầu tối ưu cho website

Mọi người xem video trước nhé

Đoạn code sau các bạn dán vào function nhé :

// start

class mvs_auto_update_tax {

function __construct() {
// add_action(‘admin_init’, [$this,’mvs_main’]);

add_action(‘save_post’, [$this,’mvs_main’]);
}

//========= main ============
function mvs_main() {
$terms = get_terms(array(
‘taxonomy’ => ‘product_cat’,
‘hide_empty’ => 1
));

if ($terms) {
foreach ($terms as $term) {
$mvs_post = new WP_Query(array(
‘post_type’ => ‘product’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘product_cat’,
‘field’ => ‘id’,
‘terms’ => $term->term_id
)
),
‘posts_per_page’ => 1
));

if ($mvs_post->have_posts()):
while($mvs_post->have_posts()):
$mvs_post->the_post();
$thumb_id = get_post_thumbnail_id();

update_term_meta($term->term_id, ‘thumbnail_id’, $thumb_id);

endwhile; wp_reset_postdata();
endif;

}
}

}

}

$mvs_obj = new mvs_auto_update_tax();

Mai Việt Sơn.

Mai Việt Sơn.

Hiện tại đang sống tại Hà Nội.
Mình có nhận làm website và là chân le ve marketing cho một công ty.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
1124 Lượt xem

Thiết kế website!

Mai Việt Sơn

Bài viết sắp ra mắt 1

Hướng dẫn tạo giỏ hàng wordpress không cần Plugin?>

02

10

3 years trước

+ Create Discussion

0
Would love your thoughts, please comment.x
()
x