'PHP Comparison Operators Inconsistent Behavior (Multilingual Wordpress navigation)
I am having a real problem with the display of a menu in Wordpress. The source of the problem is a class added in nav-walker.php using a comparison operator.
Strangely, the wrong class is selected only in one specific instance of the site: i.e the menu display in the French version of the site on the live server. There is no problem with the classes selected for the English nav on the live server. Furthermore, on my local server, the nav displays properly in both the French and English.
As you can see by the function below, from nav-walker.php, the "if" statement checks if the menu item has a parent. When it does it adds the class "dropdown-item". When it doesn't it adds the class "nav-item". In my navigation none of the items has a parent, that is menu_item_parent is 0.
public function cssClasses($classes, $item) {
if ($item->menu_item_parent != 0) {
$classes[] = 'dropdown-item';
}
if ($item->menu_item_parent == 0) {
$classes[] = 'nav-item';
}
}
So in all the instances except the French version of the nav on the live server the nav items have the proper class ; in the French version the class is wrong . Yet, the navigation structure is consistent in all versions, i.e. no children.
To check the value of menu_item_parent in the French version of the nav on the live server I did var_dump($item); right after if($item->menu_item_parent == 0){}. The value for the French live nav item is given as follows: ["menu_item_parent"]=> string(0) "", that is, it is empty, while on the English live nav, it is ["menu_item_parent"]=> string(1) "0", that is 0. This is the same on my local for each of the languages.
I checked in the database via phpAdmin: in the sp_postmeta table the meta_key _menu_item_menu_item_parent for the posts in the menu in English are "0" and in French are "null", which confirms the var_dump there.
This seems to be a problem with the WPML syncMenu feature. Anyone have any other ideas?
Solution 1:[1]
Awaiting someone who might have an insight into why the WPML syncMenu is giving a null value instead of 0, I rewrote the if statements in the function as follows to account for the null value:
if ($item->menu_item_parent != 0 and !empty($item->menu_item_parent)) {
// NOTE: the $item is the post, here we check if the post has a parent, if it does not, the value of menu_item_parent will be something other than zero. To be sure that the menus created by WPML sync, where the value of menu_item_parent is null if the page does not have a parent, we also have to make sure that the value is not null.
$classes[] = 'dropdown-item';
}
if ($item->menu_item_parent == 0 || empty($item->menu_item_parent)) {
// NOTE: in menus created by WPML menu sync, the value of menu_item_parent for menu items that don't have a parent is null rather than 0, so we have to add if empty to if statement to be sure that this class is added
$classes[] = 'nav-item';
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Don Goodes |
