Bài viết liên quan trong wordpress

banner home FINAL 1050x121 1
huong dan su dung wordpress 2

1. Hiện thị bài viết liên theo tag.

Có nghĩa những bài viết nào cùng tag với bài viết hiện tại sẽ xuất hiện.

Bạn vào trong file single.php, thông thường là sẽ ở dưới cùng sau khi kết thúc nội dung bài viết chính chúng ta sẽ đặt các bài viết liên quan tại đây. Dĩ nhiên bạn có thể để vào bất kì chỗ nào bạn muốn hiển thị.

<!-- Hiển thị bài viết theo Tag -->
<div id="relatedposttags"> 
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) 
{
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
// lấy danh sách các tag liên quan
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID), // Loại trừ bài viết hiện tại
'showposts'=>5, // Số bài viết bạn muốn hiển thị.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) 
{
echo '<h3>Bài viết liên quan</h3><ul>';
while ($my_query->have_posts()) 
{
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
</div>

2. Hiện thị bài viết liên theo category

<?php

    $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),

        'showposts'=>5, // Số bài viết bạn muốn hiển thị.

        'caller_get_posts'=>1

        );

        $my_query = new wp_query($args);

        if( $my_query->have_posts() ) 

        {

            echo '<h3>Bài viết liên quan</h3><ul class="list-news">';

            while ($my_query->have_posts())

            {

                $my_query->the_post();

                ?>

                <li>

                <div class="new-img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(85, 75)); ?></a></div>

                <div class="item-list">

                <h4><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>

                <?php the_excerpt(); ?>

                </div>

                </li>

                <?php

            }

            echo '</ul>';

        }

    }

?>

Chúc các bạn thành công! Có chi thắc mắc thì cứ cmt bên dưới nó sẽ thông báo về facebook/zalo của mình, mình sẽ hỗ trợ.

0/5 (0 Reviews)

# KHÁM PHÁ CÁC HASHTAG HÀNG ĐẦU

Theo dõi và cập nhật tin tức AZ9s thông qua các kênh truyền thông:

- Zalo Channel

- Facebook Channel

- Youtube Channel

banner home FINAL 1050x121 1
0 0 votes
Article Rating
Nhận thông báo qua Email
Nhận thông báo cho
guest

0 Comments
Inline Feedbacks
View all comments
0
Hãy để lại bình luận của bạn!x
()
x