Hello mọi người. Nay share đoạn code giúp tạo nút like và share lên facebook cho website wordpress.
Facebook đã và đang là nền tảng được rất nhiều người sử dụng. Từ trẻ em cho tới người già :v.
Luyên thuyên vài câu cho bài nó có chữ.
Oke zô thẳng code.
Tạo 2 nút like share trên facebook
Bỏ code bên dưới vào function.php
1 2 3 4 5 6 7 8 9 10 | add_action( 'woocommerce_before_add_to_cart_quantity' , 'isures_likeshare_btn_func' ); function isures_likeshare_btn_func() { echo '<div class="isures-btn--like_wrap">' ; echo '<div class="fb-like" data-href="' . get_permalink() . '" data-width="" data-layout="button_count" data-action="like" data-size="small" data-share="true"></div>' ; echo '</div>' ; } |
Ở trên mình sử dụng hook woocommerce_before_add_to_cart_quantity nên vị trí hiển thị của 2 nút này sẽ nằm trước quantity add to cart button.
Nếu mọi người không thích có thể tạo thành shortcode = cách thay thế dòng 1 ở trên bằng
1 | add_shortcode( 'isures_likeshare' , 'isures_likeshare_btn_func' ); |
và sử dụng shortcode [isures_likeshare] đặt ở nơi nào bạn muốn.
Ngoài ra có thể tham khảo các hook khác tại trang chi tiết sản phẩm ở đây
Thêm mã script của facebook vào footer
Chèn code dưới vào function.php
1 2 3 4 5 6 7 8 9 10 | add_action( 'wp_footer' , 'isures_likeshare_reg_script' , 9999 ); function isures_likeshare_reg_script() { if ( is_product() ) { echo '<div id="fb-root"></div><script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v6.0"></script>' ; } } |
THIẾT KẾ WEB 68 – WEB68
EMAIL: info.thietkeweb68@gmail.com
SĐT: 0359373636 – 0902229139
WEBSITE: thietkeweb68.net