how to add data attribute to a specific <li> element in wordpress nav menu

142
April 04, 2021, at 1:20 PM

I'm using wordpress and trying to add a data-attribute to just 1 main menu item. Specifically to a < li> element. I saw this link (how to add data attribute to a <li> element in wordpress nav menu) and I'm trying to narrow it down as it currently (and obviously targets every < li> item).

Here is my code that works just fine, as the correct behaviour is only triggered when the plugins' associated class is also present (which is only in that 1 item). But I want to learn the more targeted approach.

function booking_menu_atts( $items, $args ) {
$dom = new DOMDocument();
$dom->loadHTML($items);
$find = $dom->getElementsByTagName('li');
foreach ($find as $item ) :
    $item->setAttribute('data-agent','any');
    $item->setAttribute('data-hide-panel','yes');
endforeach;
return $dom->saveHTML();}
add_filter('wp_nav_menu_items', 'booking_menu_atts', 10, 3);

This works, but how do I target a < li> menu item with either a specific class or a specific < li> menu id

Not sure if the language I've used it clear enough, but I want to improve, thanks in advance.

Answer 1

Try to give your "li" some ID and instead of:

$find = $dom->getElementsByTagName('li');   

Try:

$find = $dom->getElementsById('#youridname'); 
Answer 2

I guess you need to add specific behavior to some menu or submenu li.

This can be easily achieved by going to

WP backend -> menus -> click on an item and below label you will have CSS Classes (optional) - here you can add class and write its respective css.

To have this, go to screen options and enable css classes - Refer to attached images

Another option how to add data attribute to a <li> element in wordpress nav menu

Rent Charter Buses Company
READ ALSO
BeautifulSoup4: Fail to find &#39;a&#39; tag with specific href value by find()

BeautifulSoup4: Fail to find 'a' tag with specific href value by find()

I am trying to crawl the realtime Bitcoin-HKD Currency from https://wwwcoinbase

54
ValueError: could not broadcast input array from shape (16,7,7,512) into shape (7,7,512)

ValueError: could not broadcast input array from shape (16,7,7,512) into shape (7,7,512)

I am building a neural network using videos as the data and I'm getting the error ValueError: could not broadcast input array from shape (7,7,3) into shape (7,7,512) when I run start training the modelI have tried different ways by changing the "X"...

182
How can I fetch and fill the resulting presigned URL for each member of each array nested two objects deep?

How can I fetch and fill the resulting presigned URL for each member of each array nested two objects deep?

really needing some help here because I am completely failing to figure this out with many hours of tryingI'm hoping I can draw on the power of stackoverflow to help me out

143
Can&#39;t get vertical images

Can't get vertical images

I'm using the following code to get vertical images and add class to it but, I don't know why, is not working:

122