Skip to main content

📡 Nhúng RSS Feed Huyền Không

Hướng dẫn nhúng nội dung phong thủy từ Huyền Không vào website của bạn

🔗 RSS Feed URL

URL RSS Feed:
https://huyenkhong.com/feed/

💡 Lưu ý: RSS feed cập nhật tự động mỗi giờ với 50 bài viết mới nhất về phong thủy.

🏆 Cách 1: WordPress Widget (Khuyến nghị)

Phương pháp tốt nhất: Sử dụng PHP server-side, không bị CORS, hiển thị ổn định

📂 BƯỚC 1: Thêm vào functions.php

Code cho functions.php:
<?php
// Function lấy RSS từ huyenkhong.com
function get_huyenkhong_rss_posts($limit = 5) {
    $cache_key = 'huyenkhong_rss_posts_' . $limit;
    $cached = get_transient($cache_key);
    
    if ($cached !== false) {
        return $cached;
    }
    
    $feed_urls = array(
        'https://huyenkhong.com/feed/',
        'https://huyenkhong.com/?feed=rss2',
        'https://huyenkhong.com/feed/rss2'
    );
    
    $posts = array();
    
    foreach ($feed_urls as $feed_url) {
        $rss = fetch_feed($feed_url);
        
        if (!is_wp_error($rss)) {
            $maxitems = $rss->get_item_quantity($limit);
            
            if ($maxitems > 0) {
                $rss_items = $rss->get_items(0, $maxitems);
                
                foreach ($rss_items as $item) {
                    $content = $item->get_content();
                    $featured_image = '';
                    if (preg_match('/<img[^>]+src=[\'"]([^\'"]+)[\'"][^>]*>/i', $content, $matches)) {
                        $featured_image = $matches[1];
                    }
                    
                    $description = strip_tags($item->get_description());
                    $description = wp_trim_words($description, 20, '...');
                    
                    $posts[] = array(
                        'title' => $item->get_title(),
                        'link' => $item->get_permalink(),
                        'date' => $item->get_date('d/m/Y'),
                        'time' => $item->get_date('H:i'),
                        'description' => $description,
                        'featured_image' => $featured_image,
                        'author' => $item->get_author() ? $item->get_author()->get_name() : 'Huyền Không'
                    );
                }
                
                set_transient($cache_key, $posts, 1800); // Cache 30 phút
                break;
            }
        }
    }
    
    return $posts;
}

// Shortcode hiển thị widget
add_shortcode('huyenkhong_widget', 'huyenkhong_widget_shortcode');
function huyenkhong_widget_shortcode($atts) {
    $atts = shortcode_atts(array(
        'limit' => 5,
        'show_debug' => false
    ), $atts);
    
    ob_start();
    
    // CSS Styles (rút gọn để demo)
    echo '<style>
    .huyenkhong-widget { font-family: Arial, sans-serif; }
    .huyenkhong-widget .post-item { display: flex; gap: 12px; margin-bottom: 12px; padding: 10px; border-radius: 6px; }
    .huyenkhong-widget .post-item.hot-post { background: linear-gradient(135deg, #fef2f2, #fee2e2); border-left: 3px solid #dc2626; }
    .huyenkhong-widget .post-thumb img { width: 80px; height: 60px; object-fit: cover; border-radius: 6px; }
    .huyenkhong-widget .post-title a { color: #333; text-decoration: none; }
    .huyenkhong-widget .post-title.hot-title a { color: #dc2626; }
    </style>';
    
    echo '<div class="huyenkhong-widget">';
    echo '<h3>🏠 Từ Huyền Không Phong Thủy</h3>';
    
    $posts = get_huyenkhong_rss_posts($atts['limit']);
    
    if (!empty($posts)) {
        foreach ($posts as $index => $post) {
            $is_hot = $index < 2;
            echo '<article class="post-item' . ($is_hot ? ' hot-post' : '') . '">';
            echo '<div class="post-thumb"><img src="' . esc_url($post['featured_image']) . '" /></div>';
            echo '<div class="post-content">';
            echo '<h4 class="post-title' . ($is_hot ? ' hot-title' : '') . '"><a href="' . esc_url($post['link']) . '" target="_blank">' . esc_html($post['title']) . '</a></h4>';
            echo '<div>📅 ' . $post['date'] . '</div>';
            echo '</div></article>';
        }
    }
    
    echo '</div>';
    
    return ob_get_clean();
}
?>

🎨 BƯỚC 2: Sử dụng Widget/Shortcode

Option 1: Custom HTML Widget

[huyenkhong_widget limit="5"]

Option 2: Trong bài viết

[huyenkhong_widget limit="3"]

Option 3: Template PHP

<?php echo do_shortcode('[huyenkhong_widget limit="5"]'); ?>

⚠️ Cách 2: JavaScript Widget (Có thể bị CORS)

Lưu ý: JavaScript có thể bị chặn bởi CORS policy. Chỉ dùng khi không thể sử dụng PHP.

💻 Widget JavaScript cải tiến

Code JavaScript:
<!-- RSS Widget Huyền Không - Layout ảnh trái -->
<div id="huyenkhong-rss-widget">
    <div style="text-align: center; padding: 20px; color: #666;">⏳ Đang tải nội dung...</div>
</div>

<script>
(function() {
    const RSS_URL = 'https://huyenkhong.com/feed/';
    const PROXIES = [
        'https://api.rss2json.com/v1/api.json?rss_url=',
        'https://api.allorigins.win/get?url=',
        'https://corsproxy.io/?'
    ];
    
    let currentProxy = 0;
    
    function loadRSSFeed() {
        const proxyUrl = PROXIES[currentProxy];
        let fetchUrl = proxyUrl + encodeURIComponent(RSS_URL);
        
        fetch(fetchUrl)
            .then(response => response.json())
            .then(data => {
                let items = [];
                if (data.items) {
                    items = data.items;
                } else if (data.contents) {
                    const parser = new DOMParser();
                    const xmlDoc = parser.parseFromString(data.contents, "text/xml");
                    const xmlItems = xmlDoc.querySelectorAll("item");
                    
                    items = Array.from(xmlItems).map(item => ({
                        title: item.querySelector("title")?.textContent || "Không có tiêu đề",
                        link: item.querySelector("link")?.textContent || "#",
                        description: item.querySelector("description")?.textContent || "",
                        pubDate: item.querySelector("pubDate")?.textContent || new Date().toISOString()
                    }));
                }
                
                if (items.length === 0) throw new Error('Không có dữ liệu RSS');
                displayWidget(items);
            })
            .catch(error => {
                currentProxy++;
                if (currentProxy < PROXIES.length) {
                    setTimeout(loadRSSFeed, 1000);
                } else {
                    showError();
                }
            });
    }
    
    function displayWidget(items) {
        let html = `<div style="font-family: Arial, sans-serif;">
            <h3 style="color: #7c3aed; margin-bottom: 15px; padding-bottom: 8px; border-bottom: 2px solid #7c3aed;">🏠 Từ Huyền Không Phong Thủy</h3>`;
        
        items.slice(0, 5).forEach((item, index) => {
            const isHot = index < 2;
            const date = new Date(item.pubDate).toLocaleDateString('vi-VN');
            const description = item.description.replace(/<[^>]*>/g, '').substring(0, 100);
            
            html += `<div style="display: flex; gap: 12px; margin-bottom: 12px; padding: 10px; border-radius: 6px; ${isHot ? 'background: linear-gradient(135deg, #fef2f2, #fee2e2); border-left: 3px solid #dc2626;' : 'border-bottom: 1px solid #eee;'}">
                <div style="width: 80px; height: 60px; background: #f3f4f6; border-radius: 6px; display: flex; align-items: center; justify-content: center; font-size: 16px; color: #9ca3af; flex-shrink: 0;">🏠</div>
                <div style="flex: 1;">
                    <h4 style="margin: 0 0 6px 0; font-size: ${isHot ? '13px' : '12px'}; font-weight: ${isHot ? '600' : '500'};">
                        <a href="${item.link}" target="_blank" style="color: ${isHot ? '#dc2626' : '#333'}; text-decoration: none;">
                            ${isHot ? '🔥 ' : ''}${item.title}
                        </a>
                    </h4>
                    <div style="font-size: 10px; color: #666; margin-bottom: 6px;">📅 ${date} • 🌐 huyenkhong.com</div>
                    <div style="font-size: 11px; color: #555; line-height: 1.3;">${description}...</div>
                </div>
            </div>`;
        });
        
        html += `<div style="text-align: center; margin-top: 15px; padding-top: 15px; border-top: 1px solid #eee;">
                    <a href="https://huyenkhong.com" target="_blank" style="background: #7c3aed; color: white; padding: 8px 16px; border-radius: 16px; text-decoration: none; font-size: 12px;">🏠 Xem thêm tại huyenkhong.com</a>
                </div></div>`;
        
        document.getElementById('huyenkhong-rss-widget').innerHTML = html;
    }
    
    function showError() {
        document.getElementById('huyenkhong-rss-widget').innerHTML = 
            `<div style="text-align: center; padding: 20px; color: #dc3545; border: 2px solid #dc3545; border-radius: 8px;">
                ❌ Không thể tải RSS feed<br>
                <a href="https://huyenkhong.com" target="_blank" style="color: #7c3aed;">Truy cập trực tiếp huyenkhong.com</a>
            </div>`;
    }
    
    loadRSSFeed();
})();
</script>

🔧 Cách 3: RSS Reader/Plugin

📰 WordPress Plugin

  • Plugin: "RSS Import" hoặc "WP RSS Aggregator"
  • RSS URL: https://huyenkhong.com/feed/
  • Cập nhật: Mỗi giờ
  • Hiển thị: 5-10 bài mới nhất

⚡ RSS Reader Services

  • Feedly: Theo dõi RSS feed
  • RSS.app: Chuyển RSS thành widget
  • RSS2JSON: API chuyển đổi
  • FeedWind: Widget nhúng trực tiếp

⚖️ So sánh các phương pháp

Phương pháp Độ ổn định Tốc độ Khó khăn Khuyến nghị
PHP Widget (Shortcode) ⭐⭐⭐⭐⭐ ⚡⚡⚡⚡⚡ 🟢 Dễ 🏆 TỐT NHẤT
JavaScript Widget ⭐⭐⭐ ⚡⚡⚡ 🟡 Trung bình ⚠️ Backup
WordPress Plugin ⭐⭐⭐⭐ ⚡⚡⚡⚡ 🔴 Khó 💼 Pro
RSS Service ⭐⭐⭐⭐ ⚡⚡ 🟢 Dễ 💰 Có phí

🆘 Hỗ trợ kỹ thuật

🛠️ Các vấn đề thường gặp

❌ CORS Error:

→ Dùng PHP Widget thay vì JavaScript

❌ RSS không tải:

→ Kiểm tra URL RSS và hosting

❌ Widget không hiện:

→ Xem Console (F12) để debug

❌ Thiếu ảnh:

→ Bài viết cần có featured image

📞 Liên hệ hỗ trợ

💬
Zalo Chat
Chat trực tiếp
📞
Hotline
0942.976969