1

I'm modified Walker_Nav_Menu_Edit class and I added three radio input boxes within the menu items. The checkboxes are work perfectly but when add new menu item show all three checkboxes unchecked. I want when add new menu item the first checkbox is checked by default.

<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemwithouticon" <?php checked($item->menuitemlayouts, 'itemwithouticon'); ?> />
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemwithicon" <?php checked($item->menuitemlayouts, 'itemwithicon'); ?> />
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemicon" <?php checked($item->menuitemlayouts, 'itemicon'); ?> />
1

You can check if the $item->menuitemlayouts has value if not set checked for the first one...

<?php echo $item->menuitemlayouts ? checked($item->menuitemlayouts, 'itemwithouticon', true) : 'checked="checked"'; ?>

In the checked() I added the 3rd arg so it won't echo twice..

<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemwithouticon" <?php echo $item->menuitemlayouts ? checked($item->menuitemlayouts, 'itemwithouticon', true) : 'checked="checked"'; ?> />
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemwithicon" <?php checked($item->menuitemlayouts, 'itemwithicon'); ?> />
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemicon" <?php checked($item->menuitemlayouts, 'itemicon'); ?> />

Or alternative just set a variable for this with default value if its not set.

<?php
$menuitemlayouts = $item->menuitemlayouts ? $item->menuitemlayouts : 'itemwithouticon';
?>
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemwithouticon" <?php checked($menuitemlayouts, 'itemwithouticon'); ?> />
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemwithicon" <?php checked($menuitemlayouts, 'itemwithicon'); ?> />
<input type="radio" name="menu-item-menuitemlayouts[<?php echo $item_id; ?>]" value="itemicon" <?php checked($menuitemlayouts, 'itemicon'); ?> />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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