<?php $tags = wp_get_post_tags(get_the_ID()); // Lấy các bài viết có liên quan if ($tags){?> <div class="top_widget top_widget2 widget_colum"> <div class="widget_colum_padding"> <h3>Bài viết liên quan</h3> <div class="widget_content"> <div class="recent_category_item"> <?php $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id; $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array(get_the_ID()), 'posts_per_page' => 5, // Số bài viết bạn muốn hiển thị ); $my_query = new wp_query($args); if( $my_query->have_posts() ): echo '<ul>'; while ($my_query->have_posts()):$my_query->the_post(); ?> <li> <a class="the_recent_post_thumbnail" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"> <?php the_post_thumbnail();?> </a> <a class="post_permalink" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> </li> <?php endwhile; echo '</ul>'; endif; wp_reset_query();?> </div> </div> </div> </div> <?php } ?>
.widget_colum { width: 100%; float: left; padding-bottom: 35px; padding-top: 10px; background: #0060a1; } .post_permalink { width: 78%; overflow: hidden; float: right; color: #fff; } .the_recent_post_thumbnail { width: 20%; overflow: hidden; float: left; } .widget_colum_padding { padding: 10px 30px; } .recent_category_item ul li { list-style: none; margin-top: 10px; margin-bottom: 10px; float: left; width: 100%; }
Khi đăng hoặc viết bài bạn nhớ thêm thẻ(tag) vào phần tag(thẻ) nằm bên phía tay phải của giao diện đăng bài trong wp-admin
Với mỗi bài viết liên quan đến một vấn đề nào đó thì bạn nhớ thêm cho chúng có chung cái thẻ(tag) này, thì khi bạn đọc bài viết nó sẽ hiển thị các bài viết có liên quan đến nhau tại vị trí mà bạn muốn.
Cảm ơn bạn đã đọc bài viết, chúc bạn thành công!