Kịch bản: lựa chọn chuyên mục tin tức trong danh mục sản phẩm, kết quả sản phẩm nào thuộc danh mục đã chọn sẽ hiển thị tin tức đã chọn
//Khởi tạo function cho shortcode
function create_shortcode_show() {
// lấy id danh mục của sản phẩm
global $product;
$product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
// do id danh mục sẽ là mảng nên vòng lặp dưới sẽ lấy lần lượt id
foreach( $product_cats_ids as $cat_id ) {
echo $cat_id;
// lấy object của danh mục sản phẩm
$term9 = get_term( $cat_id, 'product_cat' );
// lấy id danh mục bài viết lựa chọn trong danh mục sản phẩm
$term4 = get_field('danh_muc_bai_viet_lien_quan', $term9 );
// lấy id danh mục bài viết
$showposts = 10;
$args = array('cat' => $term4, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts() && $term4) : while (have_posts()) : the_post(); ?><a class="baivietlienquan" href="<?php the_permalink() ?>" rel="bookmark">
<!--?php the_post_thumbnail( 'thumbnail' ); ?--></a>
<!--?php endwhile; ?-->
<!--?php endif; ?-->
<?php
}
}
//Tạo shortcode tên là [test_shortcode] và sẽ thực thi code từ function create_shortcode
add_shortcode( 'test_show', 'create_shortcode_show' );