如何仅在网站首页隐藏指定按钮元素?
实现仅在首页隐藏按钮的方案
要实现仅在网站首页隐藏这两个移动端自定义菜单按钮,你可以在现有模板逻辑中添加首页判断条件,当处于首页时不渲染按钮代码即可。
修改后的代码示例
<div class="mobile-bar sticky-bar"> {% if j3.settings.get('mobileCustomMenuStatus1') and not is_homepage %} <a class="mobile-custom-menu mobile-custom-menu-1" href="{{ j3.settings.get('mobileCustomMenuLink1.href') }}" {{ j3.linkAttrs(j3.settings.get('mobileCustomMenuLink1.attrs')) }} style="margin-left:10px; margin-right:-30px"> {{ j3.countBadge(j3.settings.get('mobileCustomMenuLink1.name'), j3.cache.update(j3.settings.get('mobileCustomMenuLink1.total')), j3.settings.get('mobileCustomMenuLink1.classes')) }} </a> {% endif %} {% if j3.settings.get('mobileCustomMenuStatus2') and not is_homepage %} <a class="mobile-custom-menu mobile-custom-menu-2" href="{{ j3.settings.get('mobileCustomMenuLink2.href') }}" {{ j3.linkAttrs(j3.settings.get('mobileCustomMenuLink2.attrs')) }}> {{ j3.countBadge(j3.settings.get('mobileCustomMenuLink2.name'), j3.cache.update(j3.settings.get('mobileCustomMenuLink2.total')), j3.settings.get('mobileCustomMenuLink2.classes')) }} </a> {% endif %} </div>
关键说明
- 首页判断变量:上述代码中使用
is_homepage作为首页标识变量,你需要根据实际模板引擎的规则替换它。比如基于路径判断可改为request.path != '/';若平台有内置首页检测变量(如部分CMS中的home或is_frontpage),直接替换即可。 - 逻辑叠加:在原有按钮启用状态判断(
j3.settings.get('mobileCustomMenuStatusX'))的基础上,新增and not 首页条件,确保仅在非首页且按钮启用时才渲染按钮。
内容的提问来源于stack exchange,提问作者Ozgur Yetgin
相关产品推荐
相关产品推荐

