In your actual code the function to add the script is never executed. You can put the function add_my_script()
wherever you want but you need to hook it into a WordPress event. The best event for this case is the wp_enqueue_scripts:
add_action('wp_enqueue_scripts', 'add_my_script');
function add_my_script() {
wp_enqueue_script(
'hide-menu', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . '/js/hide-menu.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
}
Your javascript code is also wrong. According to jQuery selectors, you are trying to select all the <a>
elements with a href attribute that contain the substring /it/noleggio-lungo-termine
, but such elements doesn’t exist in your HTML when the laguage is not italian. The following code simply works to hide the li
element with id=menu-item-9452
:
jQuery("li#menu-item-9452").hide();
Read jQuery selectors documentation and use a valid selector.
To hide that element for certain languages, you will have to get access to the current language. I see that you are using qTranslate. A possible solution:
add_action('wp_enqueue_scripts', 'add_my_script');
function add_my_script() {
wp_enqueue_script(
'hide-menu', // name your script so that you can attach other scripts and de-register, etc.
get_template_directory_uri() . '/js/hide-menu.js', // this is the location of your script file
array('jquery') // this array lists the scripts upon which your script depends
);
$scriptData = array(
'lang' => qtrans_getLanguage(),
);
wp_localize_script('hide-menu','script_data', $scriptData);
}
Ant then in hide-menu.js:
if( script_data.lang != 'it' ) {
jQuery("li#menu-item-9452").hide();
}
Also you can hide that item by CSS. For example, in the header.php of your theme you can put this code between <head>
and </head>
:
<?php
if( qtrans_getLanguage() != 'it' ) {
echo "<style>#menu-item-9452 { display: none; } </style>";
}
?>