I need to put the PHP <?php global $' . $icon_var . '; echo $' . $icon_var . '; ?>
inside the variable of a custom menu structure, but it gets commented:
<a href="http://slembas.esy.es/">
<!--?php global $icon_home; echo $icon_home; ?-->
<span>Page Name</span>
</a>
Explanation of the variables:
$icon_var
- gets value from a custom input _menu_custom_item
where I write variable without $
which contain SVG code to make menu items fancier with icons.
function sidebar_menu() {
$menu_name = 'sidebar_pages';
if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = wp_get_nav_menu_items( $menu->term_id );
$menu_list = '<nav>' . "\n";
$menu_list .= "\t\t\t\t" . '<ul>' . "\n";
foreach ( (array) $menu_items as $key => $menu_item ) {
$icon_var = get_post_meta( $menu_item->ID, '_menu_item_custom', true );
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "\t\t\t\t\t" . '<li><a href="' . $url . '"><?php global $' . $icon_var . '; echo $' . $icon_var . '; ?><span>' . $title . '</span></a></li>' . "\n";
}
$menu_list .= "\t\t\t\t" . '</ul>' . "\n";
$menu_list .= "\t\t\t" . '</nav>' . "\n";
}
else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
The desired output should look like:
<li>
<a href="/" class="icon" title="Главная">
<svg style="width:24px;height:24px" viewBox="0 0 24 24">
<path fill="#000000" d="M10,20V14H14V20H19V12H22L12,3L2,12H5V20H10Z"></path>
</svg>
<span>Главная</span>
</a>
</li>