• 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
A A
0
3673
658
SHARES
2.1k
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
4.4/5 - (147 bình chọn)
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.

Tags: WordPressbài viết liên quanflatsome
Share263Tweet165
Minh Duy Solutions

Minh Duy Solutions

Bài viết liên quan

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.5k
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.5k
flatsome theme 1
Hướng dẫn Website

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

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

Chống spam cho plugin Contact Form 7

19/06/2024
1.5k
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
fix loi chieu ngang phien ban tren dien thoai cua theme flatsome 4075
Hướng dẫn Website

Fix lỗi chiều ngang phiên bản trên điện thoại của theme Flatsome

15/12/2023
2k

Để 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.

Cpanel là gì

cPanel là gì và hướng dẫn sử dụng cPanel hiệu quả cho người mới

23/09/2021
1.5k
phan tu cua wordpress 1

Xử lý lỗi không đăng bài viết khi lên lịch trên WordPress

15/06/2023
1.6k

Trending.

cach tao doi quoc gia cho tai khoan google 4082

Cách tạo (đổi quốc gia) cho tài khoản Google

24/01/2024
14.3k
57acc582dde37df0a76107d2fb2448d8

Cách Ngắt Link Ảnh Trong Adobe Illustrator, Đơn Giản, Hiệu Quả

04/10/2022
14.6k
cach tao doi quoc gia cho tai khoan google 4082

Cách xóa hồ sơ thanh toán tài khoản Google

24/01/2024
3.9k
huong dan cai dat laragon moi truong phat trien website de dang va nhanh chong tren windows 4053 2

Hướng dẫn cài đặt Laragon – Môi trường phát triển website dễ dàng và nhanh chóng trên Windows

07/12/2023
5.2k
Microsoft Support and Recovery Assistant

Hướng dẫn gỡ bỏ tận gốc Microsoft Office ra khỏi máy tính bằng phần mềm của Microsoft

22/07/2024
1.9k
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

  • aaPanel
  • Góc ICT
  • Thiết Kế Đồ Hoạ
  • SEO
  • Hướng dẫn WHMCS
  • Hướng dẫn Hosting
  • Hướng dẫn WordPress
  • Email Zimbra
  • Hướng dẫn DirectAdmin
  • Hướng dẫn Hosting cPanel
  • 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 © 2021 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

Add New Playlist

×
    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 © 2021 Hướng dẫn Minh Duy Solutions