// Code tạo shortcode bài viết liên quan cho Webdy
function webdy_related_post($content) {
if(is_singular('post')) {
global $post;
ob_start();
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>6,
'ignore_sticky_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {?>
<div class="relatedcat">
<div class="row related-post">
<?php while ($my_query->have_posts()):$my_query->the_post(); ?>
<div class="col large-4">
<a href="<?php echo get_the_permalink(); ?>" title="<?php echo get_the_title(); ?>">
<div class="feature">
<div class="image" style="background-image:url(<?php echo get_the_post_thumbnail_url();?>);"></div>
</div>
</a>
<div class="related-title post-title">
<a href="<?php echo get_the_permalink(); ?>" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a>
</div>
<div class='cat-label'>
<?php $post = get_post();
$cats = array();
foreach (get_the_category($post_id) as $c) {
$cat = get_category($c);
array_push($cats, $cat->name);
}
if (sizeOf($cats) > 0) {
$post_categories = implode(', ', $cats);
}
echo $post_categories;
?>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php } // end if has post
} // end if $categories
$related_post = ob_get_contents();
ob_end_clean();
return $content.$related_post;
} //end if is single post
else return $content;
}
add_shortcode('webdy_related_post', 'webdy_related_post');