简体   繁体   English

Wordpress导航 - 主动样式(CSS)

[英]Wordpress Navigation - Active Styling (CSS)

The website I'm working on is available at ryanbenson.info/wordpress/ 我正在研究的网站可在ryanbenson.info/wordpress/上找到。

I converted my theme into a WordPress theme, and I'm not quite sure how to make my navigation work the same way. 我将我的主题转换为WordPress主题,我不太确定如何使我的导航工作方式相同。

On the previous version, located at ryanbenson.info, the individual pages call for different CSS documents, fading the navigation. 在以前的版本,位于ryanbenson.info,各个页面调用不同的CSS文档,淡化导航。

** note that the navigation is images, the top one fading as you hover **请注意,导航是图像,当您悬停时,顶部的图像会逐渐消失

Now that the CSS and the header documents are standard for the whole site, I don't know how to go about styling it. 现在CSS和头文件是整个网站的标准,我不知道如何设计它。

It somewhat depends on how you're doing things, but a general solution is: 这在某种程度上取决于你是如何做事的,但一般的解决方案是:

  1. Check the current URL. 检查当前的URL。

  2. If the link is to that URL, apply a class that makes it faded. 如果链接指向该URL,请应用使其渐变的类。

It looks like this menu is baked into your header rather than using any of WordPress's menu functions. 看起来这个菜单被烘焙到你的标题中,而不是使用任何WordPress的菜单功能。 So if your code looks something like this: 所以如果你的代码看起来像这样:

<a href="/">Home</a>
<a href="/about">About</a>
<a href="/work">Work</a>
<a href="/contact">Contact</a>

You'd instead have something like: 你会有类似的东西:

<a <?= is_home() ? 'class="current_page"' : '' ?> href="/">Home</a>
<a <?= is_page('about') ? 'class="current_page"' : '' ?> href="/about">About</a>
<a <?= is_page('work') ? 'class="current_page"' : '' ?> href="/work">Work</a>
<a <?= is_page('contact') ? 'class="current_page"' : '' ?> href="/contact">Contact</a>

A good idea would be to integrate the nav_menu function into your theme. 一个好主意是将nav_menu函数集成到您的主题中。

First you have to register the menu in your functions.php 首先,您必须在functions.php中注册菜单

function register_my_menu() {
  register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );

Then you have to replace your current menu in the header.php with this: 然后你必须用headertp替换header.php中的当前菜单:

<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

Then go to wp-admin -> Appearance -> Menus and add your menu. 然后转到wp-admin - > Appearance - > Menus并添加菜单。 Now you have a working menu with classes for the current menu item. 现在,您有一个包含当前菜单项的类的工作菜单。 You can now style your new menu like it was before and have the current menu item highlighted. 您现在可以像以前一样设置新菜单的样式,并突出显示当前菜单项。 I would suggest you use css-sprites or even better an icon font instead of the pictures. 我建议你使用css-sprites甚至更好的图标字体而不是图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM