Tips for concealing an entire menu on WP, with the exception of items labeled as current-menu-ancestor

On my Wordpress site, I am faced with a simple problem to solve. I have a primary menu at the top and a secondary menu on the left side. While the top menu only contains level 1 items, the left menu has all the items. To customize the left menu, I am utilizing the default "Custom menu" widget.

To streamline the appearance, I want to display only the relevant items on the left, specifically those with the class "current-menu-ancestor."

  1. What is an elegant way to achieve this, by dynamically generating only necessary HTML code? This could involve creating a custom function in functions.php (utilizing a Twenty Eleven child theme) or using an "Advanced Custom Menu" plugin if available.

  2. I have attempted a CSS approach and made progress. By selecting green for items I wish to show and red for those I want to hide, I encountered difficulties applying a CSS rule like display: none to hide the red items without affecting the green ones.

This is the current state of my CSS:

#secondary ul {background: red;}

#secondary li.current-menu-ancestor ul,
#secondary li.current-menu-item ul {background: green;}

Visit to see a sample page illustrating the red items I aim to hide and the green items I intend to keep visible.

The goal is to have only the green items displayed on the left menu.

Answer №1

Sure, I've implemented a CSS-only solution for the problem. However, I'm open to exploring a WordPress-specific approach if anyone has one:

#secondary li a {
display: none;
}

#secondary li.current-menu-item ul a,
#secondary li.current-menu-ancestor ul a {
display: inherit;
}

Answer №2

If you need to conceal an element using CSS, simply apply the display: none; style.

In the case of a menu, it's advisable to employ Javascript to modify the css properties of the hidden element(s) when certain criteria are fulfilled (e.g., hover or click).

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular is not properly integrating Bootstrap features

I've been attempting to create bootstrap accordion items, but unfortunately they're not functioning correctly as shown in the Bootstrap documentation or YouTube tutorials. In my angular.json file, I have included both bootstrap and jQuery for te ...

Is there a method to automatically refresh all active webpages as my Firebase Realtime database accumulates new data?

With the power of Firebase, I created a chatroom using HTML that saves all its data, including usernames and messages, in a real-time Firebase database. The chatroom currently displays sent messages upon manual page reloads or when a new message is sent, ...

Challenges with Table and <p> Spacing in HTML and CSS

My website is centered around tables. The body section of my site aligns with the bottom of the header nav bar. However, when I begin typing text in the body, it shifts down by a few pixels. Here are some examples: Before typing any text: After entering ...

Equal Sized <li> elements within Bootstrap Cards

I've been working with the following "template", and there are multiple cards like this on the settings page I'm currently developing. <!-- Card template --> <div class="card mt-3 shadow-sm"> <div class="card-hea ...

Stop HTML elements from shifting position when content is modified using JavaScript

'use strict'; const countdown = () => { // create function to calculate time until launch in Days/Hours/Minutes/Seconds // time difference const countDate = new Date('May 25, 2024 00:00:00').getTime(); const now = new Date( ...

Display issue on Safari when using flexbox?

I'm currently experiencing an issue with uploading a webpage to my domain, specifically on Safari. I have applied all the necessary prefixes, but it seems that the error lies with the display: flex property. When viewed on Chrome, the page looks like ...

Guide to ensuring a jQuery modal box appears above other page elements

As I work on a jQuery popup modal box, I am faced with the issue of it pushing all other content aside when called. The toggleSlide() function is being used and works fine, but the layout problem remains. Attempts to rectify this by using position: relati ...

I'm struggling to create the CSS regular expression in Selenium for a specific piece of code

I am currently working on writing a CSS regular expression for the following line of code: "<a href="https://mail.rediff.com/cgi-bin/login.cgi" title="Already a user? Sign in" class="signin" xpath="1">Sign ...

What is the best way to ensure that two contact fields are capable of adapting

Looking for a way to center two fields ("Call Us" and "Our Email") while also ensuring they are responsive on different screen sizes? The goal is to have them stack one on top of the other when viewed on a small screen. Even though inline CSS has been used ...

Tips for aligning images and text in the center of a row using Bootstrap

I am struggling to center the image along with the text in my code. I attempted to use a container, but it did not have the desired effect. Below is the image: https://i.sstatic.net/fnaj4.png <section id="features"> <!-- <div ...

Issues with the alignment of the navbar-right in Bootstrap are causing

Just starting out with bootstrap and encountered an issue I have two different classes for the navbar-header, one I want on the left and the other on the right. I need both to be responsive. <nav class="navbar navbar-default navbar-fixed-top topnav " ...

Move the WordPress "uploads" directory to a different server

I am managing four servers, one of which has Wordpress installed for selling digital files. To enhance security measures, I prefer not to store the files on the same server as Wordpress. My goal is to relocate the "Uploads" folder to the remaining three se ...

Display a JSON object on a web browser

I am trying to display a JSON object on a web browser using HTML. The object is already in a text file and has been properly formatted for readability. My goal is to maintain the same formatting when displaying it on the browser. ...

Unable to get the Gtranslate function to function properly within the drop-down menu

Currently, I am using Gtranslate.io languages on my website with flags displayed without a drop-down menu. Everything is running smoothly but now I am looking to enhance the user experience by placing the flags inside a drop-down list. I want English to ...

Adjust the navigation height to be proportional to the main content size

I've been attempting to make my navigation menu take up the entire page, but I have yet to achieve success: My goal is for the green menu section to extend down to the footer. Here is the HTML code: <div ...

Is this code in line with commonly accepted norms and standards in Javascript and HTML?

Check out this Javascript Quiz script I created: /* Jane Doe. 2022. */ var Questions = [ { Question: "What is 5+2?", Values: ["7", "9", "10", "6"], Answer: 1 }, { Question: "What is the square root of 16?", Values: ["7", "5", "4", "1"], Answer: ...

Troublesome tab key function in FireFox causing navigation issues

I'm facing an issue with the tab key focus on links in FireFox. Strangely, it's working fine in Chrome but in FireFox, it keeps looping within one element. For better understanding, I have created a demo showcasing this behavior specifically in ...

Getting the full referrer URL can be achieved by using various methods depending

I am currently working with ASP.Net. My goal is to retrieve the complete referrer URL when visitors arrive at my website from: https://www.google.co.il/webhp?sourceid=chrome-instant&rlz=1C1CHEU_iwIL457IL457&ion=1&espv=2&ie=UTF-8#q=%D7%90 ...

Does the media query max-width refer to the size of the viewport or the size of the window?

Can someone clarify whether the max-width in a media query is based on the viewport size or window size? For instance, consider this media query: @media screen and (max-width: 360px){} Would this media query be activated when the viewport reaches 360px ...

React - Styling with Pseudo Element on Input Element not displayed properly

I'm struggling to understand why an ::after element is not displaying on an input element, but is working fine on a div. I'm attempting to create a performance-friendly focus effect for an input element, where it glows when in focus using the af ...