Post này sẽ tổng hợp cho anh em cách tạo và hiện thị Custom Post Type có tham số để anh em sử dụng nhé.

1. Tạo
Dùng Plugin ACF cho nhanh ngồi tạo funtions mất công lắm nhanh gọn nhẹ.
2. Hiện thị theo danh mục
Mình đang muốn tạo 1 shortcode để gọi các post type theo danh mục (tham số ở đây là ID danh mục) nên mình viết đoạn code này, anh em có thể tùy biến các tham số truyền vào nhé.
// TẠO SHORTCODE HIEN THI BAI VIET
function create_shortcode_randompost($ts) {
$nguyenson_query = new WP_Query(array(
'posts_per_page' => 6,
'post_type' => 'featured_item',
'operator' => 'IN',
'orderby' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'featured_item_category', // taxonomy name
'field' => 'term_id', // term_id, slug or name
'terms' => $ts['idcat'], // term id, term slug or term name
)
)
));
ob_start();
if ( $nguyenson_query->have_posts() ) :
"<ol>";
while ( $nguyenson_query->have_posts() ) :
$nguyenson_query->the_post();
?>
<div class="duan-col">
<div class="hinhanh_duan">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail("thumbnail");?></a>
<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<?php the_excerpt(); ?>
</div>
<div class="thontin_duan">
<ul>
<li><?php echo get_post_meta(get_the_ID(), 'name_duan', TRUE); ?></li>
<li><?php echo get_post_meta(get_the_ID(), 'dientich_duan', TRUE); ?></li>
<li><?php echo get_post_meta(get_the_ID(), 'diachi_duan', TRUE); ?></li>
<li><?php echo get_post_meta(get_the_ID(), 'donvi_thietke', TRUE); ?></li>
<li><?php echo get_post_meta(get_the_ID(), 'gia_duan', TRUE); ?></li>
<li><?php echo get_post_meta(get_the_ID(), 'phongcach_duan', TRUE); ?></li>
</ul>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
"</ol>";
endif;
$list_post = ob_get_contents(); //Lấy toàn bộ nội dung phía trên bỏ vào biến $list_post để return
ob_end_clean();
return $list_post;
}
add_shortcode('nguyenson_post', 'create_shortcode_randompost');

