Site icon Init HTML

Tự thêm Bài viết liên quan trong WordPress

Bạn có thể thấy pageview của bạn sẽ tăng đáng kể khi tiến hành hiển thị Bài viết liên quan vào trang.

Bài viết cùng Chuyên mục (Category)

Ví dụ lấy 5 bài viết cùng (các) chuyên mục của bài viết hiện tạikhông bao gồm bài viết hiện tại. Đặt đoạn mã sau vào single.php, sau câu lệnh the_post();.

<?php
    $cats = array();
    $categories = get_the_category();
    foreach ($categories as $cat) $cats[] = $cat->term_id;
    query_posts(array('category__in' => $cats, 'posts_per_page' => 5, 'post__not_in' => array(get_the_ID())));
    if (have_posts()) : while (have_posts()) : the_post();
?>
<div>
    <a href="<?php the_permalink(); ?>">
        <?php if (has_post_thumbnail()) the_post_thumbnail('medium', array('alt' => get_the_title())); ?>
        <span><?php the_title(); ?></span>
    </a>
</div>
<?php endwhile; else : echo '<p>Không có bài viết.</p>'; endif; wp_reset_query(); ?>

Bài viết cùng Thẻ (Tag)

<?php
    $tags = array();
    $post_tags = get_the_tags();
    foreach ($post_tags as $cat) $tags[] = $cat->term_id;
    query_posts(array('tag__in' => $tags, 'posts_per_page' => 5, 'post__not_in' => array(get_the_ID())));
    if (have_posts()) : while (have_posts()) : the_post();
?>
<div>
    <a href="<?php the_permalink(); ?>">
        <?php if (has_post_thumbnail()) the_post_thumbnail('medium', array('alt' => get_the_title())); ?>
        <span><?php the_title(); ?></span>
    </a>
</div>
<?php endwhile; else : echo '<p>Không có bài viết.</p>'; endif; wp_reset_query(); ?>

Chúc các bạn thành công!

Exit mobile version