John Davidson

PHP Comparison Operators Inconsistent Behavior (Multilingual Wordpress navigation)

0 comments
Message:


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?



My Answer:
It seems like the issue might be related to how WPML handles the synchronization of menu items between different language versions. Since the menu items in the French version have a menu_item_parent value of "null" instead of "0", it is causing the comparison operator in the nav-walker.php file to behave inconsistently.

One possible solution could be to modify the comparison operator in the cssClasses function to check for both "0" and "null" values for the menu_item_parent property. You can update the function as follows:

php
public function cssClasses($classes, $item) {
if ($item->menu_item_parent == 0 || $item->menu_item_parent == 'null') {
$classes[] = 'nav-item';
} else {
$classes[] = 'dropdown-item';
}
}


By checking for both "0" and "null" values, you can ensure that the correct class is added to the menu items in both the English and French versions of the navigation.

Alternatively, you may also want to investigate further into how WPML handles the synchronization of menu items and see if there are any settings or configurations that can be adjusted to ensure consistency in the menu structure across different language versions.

I hope this helps resolve the issue with the inconsistent behavior of the comparison operator in your multilingual Wordpress navigation.

Rate this post

4 of 5 based on 4191 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us