Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the plugin qTranslate and I have to hide one menu item when the language isn't Italian.

Searching online I created this solutionand applied the solution in this post

I created the file hide-menu.js and the content is

 jQuery('a[href*="/it/noleggio-lungo-termine"]').each(function() {
   $("li#menu-item-9452").hide();
});

Then I added in the bottom of the function.php this code

   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
    );
   }

but this solution doesn't work.

How can I fix this issue?

share|improve this question
    
Try this instead –  E Carter Young Jun 9 at 14:29

1 Answer 1

up vote 2 down vote accepted

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>";

  }

?>
share|improve this answer
    
+1. I completely missed that when I edited the post. :-) –  Pieter Goosen Jun 9 at 17:47
    
thanks for the reply, it doesn't work :( –  Mma87 Jun 12 at 7:23
    
What do you mean by "it doesn't work"? It is the javscript file loaded? If not, check that the location of hide-menu.js file is correct in wp_enqueue_script() function. If the file is correctly loaded you will have to check your javascript code. If you post the HTML of the element you want to hide I can help with js code as well. –  cybmeta Jun 12 at 8:10
    
I mean that the menu isn't hide :( the link is this milanoexclusiverental.net I need to hide the top menu item "Noleggio Lungo Termine" when the language isn't italian. –  Mma87 Jun 16 at 7:25
1  
thank you very much! I used the alternative with css and now work fine :) –  Mma87 Jun 20 at 10:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.