Dưới đây là mã plugin WordPress hoàn chỉnh sử dụng shortcode (không phải widget) để hiển thị các bài đăng mới nhất từ blog với JSON feed từ URL mới:
- Tạo file PHP (ví dụ: blogspot-recent-posts-shortcode.php) trong thư mục wp-content/plugins của WordPress.
- Dán toàn bộ mã dưới đây vào file đó:
phpSao chépChỉnh sửa<?php
/*
Plugin Name: Blogspot Recent Posts Shortcode
Plugin URI: http://example.com/
Description: Hiển thị bài đăng mới nhất từ hocdethi.tranganhnam.xyz với ảnh đại diện và trích dẫn tối đa 222 từ thông qua shortcode
.
Version: 1.0
Author: Your Name
Author URI: http://example.com/
License: GPL2
*/
function blogspot_recent_posts_shortcode() {
ob_start();
?>
<div id="blogspot-recent-posts-shortcode">
<!-- Nội dung bài đăng sẽ được load vào đây -->
</div>
<script type="text/javascript">
// Hàm callback xử lý JSONP từ feed Blogger
function displayRecentPostsBlogspot(json) {
var entries = json.feed.entry;
var html = "";
if (!entries) {
html = "<p>Không có bài đăng nào.</p>";
} else {
// Số lượng bài đăng hiển thị: 5 (có thể điều chỉnh nếu cần)
var numPosts = Math.min(entries.length, 5);
for (var i = 0; i < numPosts; i++) {
var entry = entries[i];
// Lấy tiêu đề bài viết
var title = entry.title.$t;
// Lấy đường link bài viết
var postUrl = "";
for (var j = 0; j < entry.link.length; j++) {
if (entry.link[j].rel === "alternate") {
postUrl = entry.link[j].href;
break;
}
}
// Lấy ảnh đại diện nếu có, nếu không dùng ảnh mặc định
var thumb = ("media$thumbnail" in entry) ? entry.media$thumbnail.url : "https://via.placeholder.com/80";
// Lấy nội dung bài viết hoặc summary
var content = "";
if ("content" in entry && entry.content.$t) {
content = entry.content.$t;
} else if ("summary" in entry && entry.summary.$t) {
content = entry.summary.$t;
}
// Loại bỏ các thẻ HTML
var plainText = content.replace(/<[^>]*>/g, "");
// Cắt trích dẫn thành 222 từ nếu vượt quá
var words = plainText.split(/\s+/);
if (words.length > 222) {
plainText = words.slice(0, 222).join(" ") + "...";
}
// Xây dựng HTML cho mỗi bài đăng
html += '<div class="blogspot-recent-post" style="margin-bottom:15px;">';
html += ' <div class="blogspot-post-thumb" style="float:left; margin-right:10px;">';
html += ' <a href="' + postUrl + '"><img src="' + thumb + '" alt="' + title + '" style="max-width:80px;"></a>';
html += ' </div>';
html += ' <div class="blogspot-post-content">';
html += ' <h3 class="blogspot-post-title" style="margin:0 0 5px;"><a href="' + postUrl + '">' + title + '</a></h3>';
html += ' <p class="blogspot-post-excerpt" style="margin:0;">' + plainText + '</p>';
html += ' </div>';
html += ' <div style="clear:both;"></div>';
html += '</div>';
}
}
document.getElementById("blogspot-recent-posts-shortcode").innerHTML = html;
}
</script>
<!-- JSON feed từ URL mới: hocdethi.tranganhnam.xyz -->
<script src="https://hocdethi.tranganhnam.xyz/feeds/posts/default?alt=json-in-script&max-results=5&callback=displayRecentPostsBlogspot"></script>
<?php
return ob_get_clean();
}
add_shortcode('blogspot_recent_posts', 'blogspot_recent_posts_shortcode');
?>
Hướng dẫn sử dụng plugin:
- Cài đặt Plugin:
- Upload file blogspot-recent-posts-shortcode.php vào thư mục wp-content/plugins của website.
- Vào Dashboard > Plugins và kích hoạt plugin “Blogspot Recent Posts Shortcode”.
- Sử dụng Shortcode:
- Chèn shortcode
- Chèn shortcode
Plugin này sẽ tải JSON feed từ URL mới (https://hocdethi.tranganhnam.xyz/feeds/posts/default?alt=json-in-script&max-results=5&callback=displayRecentPostsBlogspot), hiển thị tối đa 5 bài đăng với ảnh đại diện (nếu có) và trích dẫn nội dung được cắt tối đa 222 từ.
Bạn có thể tùy chỉnh CSS hoặc số lượng bài đăng bằng cách chỉnh sửa trực tiếp trong mã PHP/JS nếu cần.
Leave a comment