• 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

Code bài viết liên quan theme Flatsome không cần plugin

Minh Duy Solutions by Minh Duy Solutions
22/11/2021
Reading Time: 4 mins read
A A
1
bai viet lien quan khong dung pl
750
SHARES
2.4k
VIEWS
Share on FacebookShare on Twitter

Hôm nay mình xin chia sẻ code bài viết liên quan cho theme flatsome, không sử dụng plugin. Sau khi dùng code sẽ có thành quả như hình sau

Code bài viết liên quan theme Flatsome không cần plugin

Chỉ cần copy công thức, bỏ vào functions.php của theme đang dùng là xong nhé.

//code bài viết liên quan theme flatsome
function flatsome_related_post($content) {
    if(is_singular('post')) {
        global $post;
        ob_start();
        $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'=>6,
                'ignore_sticky_posts'=>1
            );
        
            $my_query = new wp_query( $args );
            if( $my_query->have_posts() ) {?>
                <div class="relatedcat">
                    <p class="related-post-title">Bài viết liên quan:</p>
                    <div class="row related-post">
                        <?php while ($my_query->have_posts()):$my_query->the_post(); ?>
                            <div class="col large-4">
                                <a href="<?php echo get_the_permalink(); ?>" title="<?php echo get_the_title(); ?>">
                                    <div class="feature">
                                        <div class="image" style="background-image:url(<?php echo get_the_post_thumbnail_url();?>);"></div>
                                    </div>                            
                                </a>
                                <div class="related-title"><a href="<?php echo get_the_permalink(); ?>" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a></div>
                            </div>
                        <?php endwhile; ?>
                    </div>
                </div>
                <style>
                    .relatedcat {
                        padding-top: 10px;
                        border-top: 1px solid #e8e8e8;
                        margin-top: 20px;
                    }
                    p.related-post-title {
                        font-size: 18px;
                        font-weight: bold;
                    }
                    .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;
                    }
                </style>
            <?php } // end if has post
        } // end if $categories
        $related_post = ob_get_contents();
        ob_end_clean();
        return $content.$related_post;
    } //end if is single post
    else return $content;
}
add_filter('the_content', 'flatsome_related_post');

 

Code bài viết liên quan hiện sidebar

Mình code theo yêu cầu của 1 bạn, cần bài viết liên quan hiện ở sidebar. Demo như sau:

Code bài viết liên quan theme Flatsome không cần pluginĐoạn code chèn vào functions.php như sau:

// code bài viết liên quan sidebar
function related_flatsome_sidebar() {
    if(is_singular('post')) {
        global $post;
        ob_start();
        $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() ) {?>
                <ul class="related-post-sidebar">
                    <?php while ($my_query->have_posts()):$my_query->the_post(); ?>
                        <li class="recent-blog-posts-li">
                            <div class="flex-row recent-blog-posts align-top pt-half pb-half">
                                <div class="flex-col mr-half">
                                    <div class="badge post-date  badge-outline">
                                        <div class="badge-inner bg-fill" style="background: url(<?php echo get_the_post_thumbnail_url();?>); border:0;"></div>
                                    </div>
                                </div>  
                                <div class="flex-col flex-grow">
                                    <a href="<?php echo get_the_permalink(); ?>" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a>
                                    <span class="post_comments op-7 block is-xsmall"><a href="<?php echo get_the_permalink(); ?>/#respond"></a></span>
                                </div>
                            </div> 
                        </li>                      
                    <?php endwhile; ?>                    
                </ul>             
                <style>
                    ul.related-post-sidebar li {
                        border-top: 1px solid #ececec;
                        margin-bottom: 0;
                    }
                    ul.related-post-sidebar li:first-child {
                        border-top: none;
                    }
                </style>
            <?php } // end if has post
        } // end if $categories
        $related_post = ob_get_contents();
        ob_end_clean();
        return $related_post;
    } //end if is single post
    else return;
}
add_shortcode('related_flatsome_sidebar','related_flatsome_sidebar');
add_filter( 'widget_text', 'do_shortcode' );

Cách sử dụng: Vào Giao diện -> Widget, kéo wiget tên là HTML vào sidebar mà bạn muốn hiện thị, chèn shortcode [related_flatsome_sidebar] vào ô nội dung và bấm Lưu. Các bạn xem hình bên dưới

Code bài viết liên quan theme Flatsome không cần plugin

Các bạn muốn trang trí lại thì sửa trong phần css style của đoạn code trên.

Chúc các bạn thêm bài viết liên quan thành công!

 

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

Minh Duy Solutions

Bài viết liên quan

binhluan
Hướng dẫn Webmaster

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

09/12/2025
1.5k
5358fe1a7da72ce25dc1c40fb518359a
Hướng dẫn Webmaster

Hướng dẫn xóa hàng loạt bản ghi DNS khỏi Cloudflare

16/07/2025
1.5k
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
Hướng dẫn Webmaster

Hướng dẫn tạo mật khẩu ứng dụng (app password) trên Gmail

27/03/2025
1.5k
Cach De Tang Toc Website WordPress Khi Su Dung Shared Hosting
Hướng dẫn Webmaster

Cách Để Tăng Tốc Website WordPress Khi Sử Dụng Shared Hosting

28/10/2024
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

Comments 1

  1. Diễn says:
    3 năm ago

    CẢM ƠN AD NỘI DUNG RẤT HỮU ÍCH

    Bình luận

Để 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ý cơ sở dữ liệu (database) trên cPanel

22/09/2021
1.6k
DirectAdmin

Tạo tài khoản Reseller trên DirectAdmin.

23/09/2021
1.5k

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
27.1k

Hướng dẫn yêu cầu Canva hủy gói đăng ký và yêu cầu hoàn tiền Canva

12/04/2025
2.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
6.1k
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
2.8k
57acc582dde37df0a76107d2fb2448d8

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

04/10/2022
16.4k
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