11.26c19v03 Blog Tag Cards with Images – Browse Posts by Tag

Want a “Browse by Topic” row above your blog? This code reads the tags from your blog and turns each List Section item into an image card with a tag label that links to the filtered posts.

11.26c19v03

#1. Install Code

#1.1. First, use List Simple

#1.2. Enable these options

11.26c19v03

#1.3. Edit Section Button > Enter Blog Page URL

11.26c19v03

#1.4. Hover on top right of section > Click EDIT SECTION > at Format > Anchor Link > enter word: team-1126c12v43

11.26c19v03

#1.5. Next, use code to Page Header Injection

<!-- 11.26c19v03 Blog Tag Cards with Images – Browse Posts by Tag -->
<script>
window.listTagCardsOptions = {
  sectionSelector: 'section[id*="team-1126c12v43"]',
  hideSectionButton: true,
  hideItemButton: true,
  hideItemsWithoutTag: true,
  imageRadius: '22px',
  imageZoom: 1.05,
  labelRadius: '50px',
  labelSpace: '9px',
  labelPadding: '8px 14px',
  labelBackground: '#ffffff',
  labelColor: '',
  scrollOnMobile: true,
  scrollBreakpoint: 767,
  scrollItemWidth: '170px',
  scrollGap: '12px'
};
</script>
<script>
(function(){
var o=Object.assign({sectionSelector:'section[id*="team-1126c12v43"]',hideSectionButton:true,hideItemButton:true,hideItemsWithoutTag:true,imageRadius:'22px',imageZoom:1.05,labelRadius:'50px',labelSpace:'9px',labelPadding:'8px 14px',labelBackground:'#ffffff',labelColor:'',scrollOnMobile:true,scrollBreakpoint:767,scrollItemWidth:'170px',scrollGap:'12px'},window.listTagCardsOptions||{});
var S=o.sectionSelector,P=':is('+S+') ',R=':is('+S+').tg-ready ul.user-items-list-item-container > li.list-item',sid='list-tag-cards-style-'+S.replace(/[^\w-]/g,''),U=P+'ul.user-items-list-item-container',I=U+' > li.list-item',cache={};
function d(p,v,k){return v!==''&&v!=null?p+':'+v+(k?'!important':'')+';':''}
if(!document.getElementById(sid)){
var css=':is('+S+'):not(.tg-done) ul.user-items-list-item-container .list-item-content__title{visibility:hidden;animation:tgShow 0s 3s forwards}@keyframes tgShow{to{visibility:visible}}'+
(o.hideSectionButton?P+'.list-section-button-container{display:none!important}':'')+
R+'.tg-item .list-item-content__text-wrapper{display:none!important}'+
(o.hideItemButton?R+'.tg-item .list-item-content{display:none!important}'+R+'.tg-item .list-item-media{margin-bottom:0!important}':'')+
(o.hideItemsWithoutTag?R+':not(.tg-item){display:none!important}':'')+
I+'.tg-item{position:relative;cursor:pointer}'+
I+'.tg-item .list-item-media{position:relative}'+
I+'.tg-item .list-item-media-inner{isolation:isolate;'+d('border-radius',o.imageRadius)+'}'+
(o.imageZoom!==''?I+'.tg-item .list-item-media-inner img{transition:transform .35s ease}'+I+'.tg-item:hover .list-item-media-inner img{transform:scale('+o.imageZoom+')}':'')+
I+' .tg-link{position:absolute;inset:0;z-index:2;display:block;text-decoration:none;background:none!important;color:inherit}'+
I+' .tg-link:focus-visible{outline:3px solid currentColor;outline-offset:4px;'+d('border-radius',o.imageRadius)+'}'+
I+' .tg-label{position:absolute;margin:0!important;max-width:calc(100% - 2 * '+(o.labelSpace||'0px')+');box-sizing:border-box;overflow-wrap:anywhere;text-align:left;'+d('left',o.labelSpace)+d('bottom',o.labelSpace)+d('padding',o.labelPadding)+d('border-radius',o.labelRadius)+d('background',o.labelBackground)+d('color',o.labelColor,1)+'}'+
(o.scrollOnMobile?'@media(max-width:'+o.scrollBreakpoint+'px){'+':is('+S+').tg-ready ul.user-items-list-item-container{display:flex!important;overflow-x:auto;padding-bottom:10px;scrollbar-width:thin;'+d('gap',o.scrollGap,1)+'}'+R+'{flex:0 0 '+o.scrollItemWidth+';width:'+o.scrollItemWidth+'!important;margin:0!important}}':'')+
'@media(prefers-reduced-motion:reduce){'+I+'.tg-item .list-item-media-inner img{transition:none}'+I+'.tg-item:hover .list-item-media-inner img{transform:none}}';
var st=document.createElement('style');st.id=sid;st.textContent=css;document.head.appendChild(st);
}
function getTags(href,n){
var u=new URL(href,location.href),key=u.pathname+'|'+n;
if(cache[key])return cache[key];
var tags=[],seen={},base=u.pathname.replace(/\/$/,'');
function page(url){
return fetch(url,{credentials:'same-origin'}).then(function(r){return r.json()}).then(function(j){
(j.items||[]).forEach(function(it){var t=it.tags&&it.tags[0];if(!t||tags.length>=n)return;var k=t.toLowerCase();if(seen[k])return;seen[k]=1;tags.push({name:t,url:base+'/tag/'+encodeURIComponent(t).replace(/%20/g,'+')})});
var nx=j.pagination&&j.pagination.nextPage&&j.pagination.nextPageUrl;
if(tags.length<n&&nx)return page(nx+(nx.indexOf('?')>-1?'&':'?')+'format=json');
return tags;
});
}
cache[key]=page(u.pathname+'?format=json').catch(function(){return []});
return cache[key];
}
function apply(sec,tags){
var ul=sec.querySelector('ul.user-items-list-item-container');if(!ul)return;
var lis=[].filter.call(ul.children,function(e){return e.classList.contains('list-item')});
lis.forEach(function(li,i){
var t=tags[i],a=li.querySelector('.tg-link');
if(!t){li.classList.remove('tg-item');if(a)a.remove();return}
li.classList.add('tg-item');
var m=li.querySelector('.list-item-media')||li;
if(!a){a=document.createElement('a');a.className='tg-link';a.innerHTML='<h2 class="list-item-content__title tg-label"></h2>'}
if(a.parentNode!==m)m.appendChild(a);
if(a.getAttribute('href')!==t.url)a.setAttribute('href',t.url);
var lb=a.querySelector('.tg-label');if(lb.textContent!==t.name)lb.textContent=t.name;
a.setAttribute('aria-label',t.name);
if(!li.tgClick){li.tgClick=1;li.addEventListener('click',function(e){var x=li.querySelector('.tg-link');if(x&&!e.target.closest('a'))x.click()})}
});
sec.classList.add('tg-ready','tg-done');
}
function run(){
document.querySelectorAll(S).forEach(function(sec){
var btn=sec.querySelector('.list-section-button-container a[href]'),ul=sec.querySelector('ul.user-items-list-item-container');
if(!btn||!ul){sec.classList.add('tg-done');return}
var n=[].filter.call(ul.children,function(e){return e.classList.contains('list-item')}).length,href=btn.getAttribute('href');
var k=href+'|'+n;
if(sec.tgKey===k&&sec.tgTags){apply(sec,sec.tgTags);return}
if(sec.tgKey===k)return;
sec.tgKey=k;
getTags(href,n).then(function(tags){if(sec.tgKey!==k)return;sec.tgTags=tags;if(tags.length)apply(sec,tags);sec.classList.add('tg-done')});
});
}
var raf=0;
function init(){run();new MutationObserver(function(){if(raf)return;raf=requestAnimationFrame(function(){raf=0;run()})}).observe(document.body,{childList:true,subtree:true})}
if(document.readyState!=='loading')init();else document.addEventListener('DOMContentLoaded',init);
})();
</script>

#1.6. Customize

You can change Tag style here

labelSpace: '9px',
  labelPadding: '8px 14px',
  labelBackground: '#ffffff',
  labelColor: '',

Change item radius, image radius, zoom ratio here

imageRadius: '22px',
  imageZoom: 1.05,
  labelRadius: '50px',

 

Buy me a coffee