开发者

WordPress Menu highlight with pagination

开发者 https://www.devze.com 2023-02-13 21:56 出处:网络
I have a WordPress website with a menu similar to this one: Yahoo news, so basically: a horizontal menu with two levels, the first with main categories the second with sub-categories (in the \"Home\

I have a WordPress website with a menu similar to this one: Yahoo news, so basically:

  • a horizontal menu with two levels, the first with main categories the second with sub-categories (in the "Home" menu item, the subcategory is the page "about us".
  • I am using the build-in Menu of WordPress

The problem I have is with the pagination and also with the search form:

  1. When I am in the Home Page and I click any pagination number, the subcategories of "Home" menu item disappears, and most importantly, the "H开发者_运维技巧ome" menu item is no longer highlighted.

  2. When I search something, again the same issue, the "Home" is no longer highlighted, and there is no subcategory under "Home" menu item.


I have found a solution:

1) Open header.php, and add this in the body tag:

<body<?php if ( is_home() || is_search() || is_404() )  { echo ' class="main"';} else { echo '';} ?>>

2) Add a class that target the home class, so:

body.main #main-menu ul li.menu-item-home a { 
    background: #313B47;
    color: white;
}

Now if I am in the home page, or in the search page, or 404 error page, WordPress adds class="main" to the body, so I can easily target the menu item which I want.

0

精彩评论

暂无评论...
验证码 换一张
取 消