Gắn biểu tượng sự kiện cạnh logo bằng shortcode
2 min read
Thủ thuật này giúp bạn dễ dàng hiển thị biểu tượng sự kiện (ví dụ: Trung Thu, 20/10, noel…) cạnh logo mà không cần chỉnh sửa lại template mỗi lần.
Giới thiệu
- Không cần sửa lại template mỗi sự kiện.
- Thêm nhanh qua widget HTML.
- Tự động ẩn khi hết event.
Code
Dán code bên dưới trong template<style id="evt-icon-base">
[data-event-slot]{display:none!important;}
@keyframes lanternSwing{0%{transform:rotate(-6deg)}50%{transform:rotate(6deg);filter:drop-shadow(0 0 10px rgba(255,235,59,.6))}100%{transform:rotate(-6deg)}}
.logotop {position: relative;display: inline-block;}
</style>
<script>
//<\[\/event\]/i);
var d=txt.match(/\[end\](.*?)\[\/end\]/i);
return e?{svg:e[1].trim(),end:d?d[1].trim():null}:null;
}
function validDate(dateStr){
if(!dateStr)return true;
var now=new Date();
var end=new Date(dateStr.replace(/(\d{2})\/(\d{2})\/(\d{4})/,"$2/$1/$3"));
return now<=end;
}
function init(){
var w=document.querySelector("[data-event-slot]");
if(!w)return;
var t=parseShortcode(w.innerHTML);
if(!t)return;
if(!validDate(t.end))return;
var logo=document.querySelector(LOGO_WRAP_SEL);
if(!logo)return;
w.style.display="none";
var h=document.createElement("span");
h.setAttribute("aria-hidden","true");
applyStyle(h);
h.innerHTML=t.svg;
logo.appendChild(h);
}
if(document.readyState!=="loading")init();
else document.addEventListener("DOMContentLoaded",init);
})();
/*]]>*/
</script>
⚠️ Lưu ý về phần tử .logotop
Nếu template của bạn dùng selector khác (ví dụ .header-logo hoặc #logo-wrap), hãy cập nhật dòng:
var LOGO_WRAP_SEL = ".logotop";
trong đoạn script cho phù hợp với cấu trúc trang của bạn.
Tạo widget HTML
Vào Bố cục → Thêm tiện ích → HTML/Javascript, dán nội dung sau:<div data-event-slot> [event] <svg viewBox="0 0 100 100"> <circle cx="50" cy="50" r="40" fill="#ff6b88"></circle> </svg> [/event] [end]31/10/2025[/end] </div>
- Bạn có thể thay phần <svg>...</svg> bằng bất kỳ biểu tượng nào cho sự kiện, ví dụ trái tim, hoa, pháo hoa, đèn lồng,…
- Và 31/10/2025 thành thời gian kết thúc event
Hoạt động tự động
- Tự động hiển thị biểu tượng cạnh logo.
- Biểu tượng lắc nhẹ nhờ animation
lanternSwing. - Hết hạn (
[end]) → tự ẩn, không cần xóa thủ công.

[pre]<div data-event-slot>
[event]
<svg viewBox="0 0 100 100">
<circle cx="50" cy="50" r="40" fill="#ff6b88"></circle>
</svg>
[/event]
[end]31/10/2025[/end]
</div>[/pre]
https://res.cloudinary.com/dxahoq7x4/image/upload/v1759995099/x2bruycd1aydjntvgoeg.jpg
[pre]<div data-event-slot>
[event]
<svg class="lantern-heart" role="img" viewbox="0 0 160 260" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"> <path d="M80 0 V48" stroke="#b94b5a" stroke-linecap="round" stroke-width="3"/> <circle cx="80" cy="52" r="6" fill="none" stroke="#b94b5a" stroke-width="3"/> <g transform="translate(80,122)"> <defs> <lineargradient id="heartGrad" x1="0" y1="0" x2="1" y2="1"> <stop offset="0%" stop-color="#ff6b88"/> <stop offset="100%" stop-color="#ff3d61"/> </stop></stop></lineargradient> </defs> <path d="M 0,52 C -50,22 -66,-10 -44,-28 C -26,-42 -6,-30 0,-14 C 6,-30 26,-42 44,-28 C 66,-10 50,22 0,52 Z" fill="url(#heartGrad)" stroke="#c2314d" stroke-width="6"/> <path d="M 0,52 C -50,22 -66,-10 -44,-28 C -26,-42 -6,-30 0,-14 C 6,-30 26,-42 44,-28 C 66,-10 50,22 0,52 Z" fill="none" stroke="rgba(255,255,255,.55)" stroke-width="10" stroke-linejoin="round"/> <g transform="translate(0,58)"> <rect x="-8" y="0" width="16" height="18" rx="3" fill="#c2314d"/> <rect x="-5" y="18" width="10" height="6" rx="2" fill="#fbc02d"/> <path d="M0 24 C -6 42, -4 56, -6 72" fill="none" stroke="#c2314d" stroke-width="4" stroke-linecap="round"/> <path d="M0 24 C 6 42, 4 56, 6 72" fill="none" stroke="#c2314d" stroke-width="4" stroke-linecap="round"/> <circle cx="-6" cy="72" r="4" fill="#fbc02d"/> <circle cx="6" cy="72" r="4" fill="#fbc02d"/> </circle></circle></path></path></rect></rect></g> <text x="0" y="18" text-anchor="middle" font-size="28" fill="#fff" font-family="ui-sans-serif,system-ui,Segoe UI,Roboto,Arial" font-weight="800">20/10</text> </path></path></g> </circle></path></svg>
[/event]
[end]21/10/2025[/end]
</div>[/pre]
Thêm vào hàm applyStyle(el) đoạn sau
[pre]el.style.width = "50px";[/pre]
Sửa 50px cho phù hợp
[pre]audio, canvas, embed, iframe, img, object, svg, video {
display: block;
vertical-align: middle;
}[/pre]
Phương án đề xuất là thêm
[pre].header-widget svg{display: inline-block!important;}[/pre]
Không hiển thị do HTML1 chắc đang chỉ cho hiển thị ở trang bài viết 😁
[pre]el.style.top="-15px";[/pre]
Thành
[pre]el.style.top="26px";[/pre]
Là đẹp 😁