• Về minhduy.vn
  • Đăng ký Hosting
  • Thiết kế Website
  • Khuyến mãi HOT
Hướng Dẫn Sử Dụng Dịch Vụ - Minh Duy Solutions
  • Trang chủ
  • Hosting
    • Hướng dẫn Hosting cPanel
    • Hướng dẫn Hosting DirectAdmin
  • Tên miền
  • Website
    • Hướng dẫn WordPress
    • Hướng dẫn Webmaster
  • Email Doanh Nghiệp
    • Office 365
    • Google Work Space
    • Email Zimbra
  • VPS – Máy chủ
    • Hướng dẫn Linux
    • Hướng dẫn DirectAdmin
  • Hướng dẫn chung
  • Góc ICT
No Result
View All Result
  • Trang chủ
  • Hosting
    • Hướng dẫn Hosting cPanel
    • Hướng dẫn Hosting DirectAdmin
  • Tên miền
  • Website
    • Hướng dẫn WordPress
    • Hướng dẫn Webmaster
  • Email Doanh Nghiệp
    • Office 365
    • Google Work Space
    • Email Zimbra
  • VPS – Máy chủ
    • Hướng dẫn Linux
    • Hướng dẫn DirectAdmin
  • Hướng dẫn chung
  • Góc ICT
No Result
View All Result
Hướng Dẫn Sử Dụng Dịch Vụ - Minh Duy Solutions
No Result
View All Result
Home Hướng dẫn Website Hướng dẫn WordPress

Hiển thị bài viết liên quan trong theme Flatsome

Minh Duy Solutions by Minh Duy Solutions
09/03/2023
Reading Time: 4 mins read
204 11
A A
0
3673
667
SHARES
2.2k
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Bài viết liên quan theo tag
  2. Bài viết liên quan cùng Chuyên mục
hien thi bai viet lien quan flatsome
Hiển thị bài viết liên quan có hình ảnh trên theme flatsome

Theme Flatsome khá tuyệt có thể ứng dụng để thiết kế website cho tất cả các ngành nghề nhưng phần blog thì lại thiếu mục các bài viết liên quan, gây khó khăn cho anh em muốn làm website tin tức. Nhiều khi sử dụng plugin thay thế khiến cho ảnh hưởng đến tốc độ tải trang.

Mình đã từng search và thử cài rất nhiều plugin hiển thị bài viết liên quan nhưng chưa thấy cái nào ưng ý, và đây chính là đoạn code đơn giản hợp với ý mình nhất.

Bài viết liên quan theo tag

Dán code này vào file functions.php của theme/child theme đang kích hoạt

 * Code bài viết liên quan theo tag
*/
function related_tag() {
    global $post;
    $tags = wp_get_post_tags($post->ID);
    if ($tags){
        $output = '<div class="related-box">';
        $first_tag =  $tags[0]->term_id;
        
        $args=array(
            'tag__in' => array($first_tag),
            'post__not_in' => array($post->ID),
            'posts_per_page'=>3,
            'ignore_sticky_posts'=>1
        );
        $my_query = new wp_query($args);
        
        if( $my_query->have_posts() ):
            $output .= '<span class="related-head">Bài viết liên quan:</span><ul class="row related-post">';
            while ($my_query->have_posts()):$my_query->the_post();
            $output .= 
                '<li class="col large-4">
                                <a href="'.get_the_permalink().'" title="'.get_the_title().'">
                                    <div class="feature">
                                        <div class="image" style="background-image:url('. get_the_post_thumbnail_url() .');"></div>
                                    </div>                            
                                </a>
                                <div class="related-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></div>
                            </li>';
            endwhile;
            $output .= '</ul>';
        endif; wp_reset_query();
        $output .= '</div>';
        return $output;
    }
    else return;
}
add_shortcode('related_tag', 'related_tag');

Bài viết liên quan cùng Chuyên mục

Dán code này vào file functions.php của theme/child theme đang kích hoạt

/*
 * Code bài viết liên quan theo chuyên mục
*/
function related_cat() {
    $output = '';
    if (is_single()) {
      global $post;
      $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'=>3,
          'ignore_sticky_posts'=>1
        );
        
        $my_query = new wp_query( $args );
        if( $my_query->have_posts() ):
            $output .= '<div class="related-box">';
                $output .= '<span class="related-head">Bài viết liên quan:</span><div class="row related-post">';
                    while ($my_query->have_posts()):$my_query->the_post();
                    $output .= 
                        '<div class="col large-4">
                            <a href="'.get_the_permalink().'" title="'.get_the_title().'">
                                <div class="feature">
                                    <div class="image" style="background-image:url('. get_the_post_thumbnail_url() .');"></div>
                                </div>                            
                            </a>
                            <div class="related-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></div>
                        </div>';
                    endwhile;
                $output .= '</div>';
            $output .= '</div>';
        endif;   //End if.
      wp_reset_query();
    }
    return $output;
  }
}
add_shortcode('related_cat','related_cat');

Tiếp theo là phần css, bạn chèn vào style.css của theme/child theme đang kích hoạt hoặc thêm vào CSS bổ sung trong Tuỳ biến của Flatsome.

 
.related-box .related-head {
    font-weight: 700;
    display: block;
    margin-bottom: 10px;
    font-size: 19px;
    color: black;
}
.related-box ul li {
    margin-bottom: 3px;
}
.related-box ul li a {
    font-weight: 700;
    font-size: 16px;
    /*color: #2a9e2f;*/
}
.related-box ul li a:hover {
    text-decoration: underline;
}
.feature {
    position: relative;
    overflow: hidden;
}
.feature::before {
	content: "";
	display: block;
	padding-top: 56.25%;
}
.feature .image{
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    margin: auto;
    background-size: cover;
    background-position: center;
}
ul.row.related-post li {
    list-style: none;
}
.related-title {
    line-height: 1.3 !important;
    margin-top: 10px !important;
}

 

Sau khi thêm đủ 2 cái đó cho con, chúng ta được 2 shortcode [”related_tag”] và [”related_cat”] bạn nhớ bỏ dấu ” đi nhé.

Chúng ta chọn 1 loại rồi thêm vào Flatsome –> Theme Option –> Blog –> Blog Single Post, chúng ta kéo xuống dưới cùng, tới phần HTML after blog posts. Chúng ta chèn shortcode vào đó rồi lưu lại là xong.

Bạn có thể xem kết quả ngay bên dưới bài viết này.

4.4/5 - (147 bình chọn)
Tags: WordPressbài viết liên quanflatsome
Share267Tweet167
Minh Duy Solutions

Minh Duy Solutions

Bài viết liên quan

stop wordpress user registration spam
Hướng dẫn WordPress

Hướng dẫn xoá hàng loạt user spam trên WordPress

23/05/2025
1.5k
thanh top woo c
Hướng dẫn Website

Hướng dẫn ẩn thanh Edit Product che mất tùy chọn khi đăng sản phẩm của WooComerce

24/09/2024
1.6k
flatsome theme 1
Hướng dẫn Website

Hướng dẫn tắt UXBuilder ở 1 số post type theme Flatsome

24/09/2024
1.6k
flatsome theme 1
Hướng dẫn Website

Bật UX Builder cho Custom post type trên theme Flatsome

24/09/2024
1.8k
Contact Form 7
Hướng dẫn Website

Chống spam cho plugin Contact Form 7

19/06/2024
1.6k
Contact Form 7
Hướng dẫn WordPress

Hướng dẫn cách truyền tiêu đề bài viết, sản phẩm vào Contact Form 7

19/06/2024
1.6k

Để lại một bình luận Hủy

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

No Result
View All Result

Recommended.

what is cpanel

Quản lý Domain (tên miền) trên cPanel

22/09/2021
1.6k
ubuntu 18 04 lts icin yolun sonuna gelindi

Cách thêm swap trên Ubuntu

24/09/2021
1.6k

Trending.

outlook là gì

Tạo rules trên Microsoft Outlook

29/05/2026
1.5k
outlook la gi cach cai dat va su dung outlook cho nguoi moi bat dau

Thay đổi thời gian cập nhật gửi nhận Email mới trên Outlook

29/05/2026
1.5k
cach tai toan bo email tren gmail ve may tinh de luu tru 4763

Cách tải toàn bộ email trên Gmail về máy tính để lưu trữ

10/04/2026
1.5k
Tên miền website là gì?

Vòng đời tên miền Việt Nam và Quốc Tế cập nhật năm 2026

07/04/2026
1.5k
binhluan

Cách ẩn comment trên Fanpage để tránh bị cướp khách hàng

09/12/2025
1.5k
Wiki Minh Duy Solutions

WIKI.MINHDUY.VN

Chuyên trang hướng dẫn sử dụng dịch vụ của Minh Duy Solutions

DMCA.com Protection Status

Danh mục

  • GSuite
  • aaPanel
  • Góc ICT
  • Thiết Kế Đồ Hoạ
  • SEO
  • Hướng dẫn WHMCS
  • Hướng dẫn WordPress
  • Email Zimbra
  • Hướng dẫn DirectAdmin
  • Hướng dẫn Hosting cPanel
  • Hướng dẫn Hosting
  • Office 365
  • Hướng dẫn Website
  • Hướng dẫn Linux
  • Hướng dẫn Hosting DirectAdmin
  • Google Work Space
  • Hướng dẫn Webmaster
  • Hướng dẫn máy chủ
  • Hướng dẫn Email Doanh Nghiệp
  • Hướng dẫn Tên miền
  • Hướng dẫn chung
  • Uncategorized

Thẻ

AFC apt-get update apt-get upgrade category chủ thể cPanel debian DirectAdmin email doanh nghiệp Environment Variables giải nén Google Workspace G Suite hosting ioncube khôi phục mật khẩu linux nginx nmap nodejs npm Office 365 OneDrive Outlook out of memory path Paypal phpmyadmin SMTP. GMail SSH sudo swap Symbolic Link tar tar.gz thông báo tên miền tên miền ubuntu user whois WooCommerce WordPress wordpress cơ bản wp rocket Zimbra
  • Về minhduy.vn
  • Đăng ký Hosting
  • Thiết kế Website
  • Khuyến mãi HOT

Copyright © Hướng dẫn Minh Duy Solutions

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
×
    No Result
    View All Result
    • Trang chủ
    • Hosting
      • Hướng dẫn Hosting cPanel
      • Hướng dẫn Hosting DirectAdmin
    • Tên miền
    • Website
      • Hướng dẫn WordPress
      • Hướng dẫn Webmaster
    • Email Doanh Nghiệp
      • Office 365
      • Google Work Space
      • Email Zimbra
    • VPS – Máy chủ
      • Hướng dẫn Linux
      • Hướng dẫn DirectAdmin
    • Hướng dẫn chung
    • Góc ICT

    Copyright © Hướng dẫn Minh Duy Solutions