Đây là một shortcode có chứa tham số anh em nhé, mình viết để tùy chỉnh hàm gọi theo danh mục bài viết sẽ tùy biến hơn.
// TẠO SHORTCODE HIEN THI BAI VIET
function create_shortcode_randompost($ts) {
$nguyenson_query = new WP_Query(array(
'posts_per_page' => 10,
'cat' => $ts['idcat'],
'orderby' => 'DESC'
));
$i=0;
ob_start();
if ( $nguyenson_query->have_posts() ) :
"<ol>";
while ( $nguyenson_query->have_posts() ) :
$nguyenson_query->the_post();
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile;
"</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');
Cách gọi shortcode thì cũng rất đơn giản thôi
[nguyenson_post idcat="2"]
sdsd