首页 > 解决方案 > Front-page.php url 蛞蝓

问题描述

我有一个非常简单的问题。我正在构建一个非常小且基本的网站,我在其中编写了一个简单的菜单。我希望菜单中的当前页面以黄色突出显示。

为了做到这一点,我添加了一个 if 语句来查找url slug,这是真的,将 CSS 类添加到那个特定的 HTML 元素。

适用于“服务”和“关于我们”,但主页呢?我为此使用什么 URL slug?此外,无论出于何种原因,我的“索引”url slug 都无法正常工作。

注意:“现代”类是改变颜色的 CSS 触发器。

 <ul class="list-inline">
            <!-- empty site_url() will default to front-page.php -->
            <li class="list-inline-item modern"><a href="<?php echo site_url()?>" class="text-white lead"><h3 class="menu-items <?php if (is_page('http://modern-artisans--carpentry-version.local/')) echo "modern"?>">home</h3></a></li>
            <li class="list-inline-item"><a href="<?php echo site_url('/services')?>" class="text-white lead"><h3 class="menu-items <?php if (is_page('services')) echo "modern"?>">services</h3></a></li>
            <li class="list-inline-item"><a href="<?php echo site_url('/about-us')?>" class="text-white lead"><h3 class="menu-items <?php if (is_page('about-us')) echo "modern"?>">about us</h3></a></li>
            <li class="list-inline-item"><a href="<?php echo site_url('/index')?>" class="text-white lead"><h3 class="menu-items <?php if (is_page('index')) echo "modern"?>">work history</h3></a></li>
          </ul>

标签: phpwordpresstwitter-bootstrap

解决方案


您应该能够通过使用返回布尔值的 is_front_page() 来实现这一点。您可以将其放在菜单中的 if 语句中。

https://developer.wordpress.org/reference/functions/is_front_page/


推荐阅读