1

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>
  • Please ask future PHP questions on StackOverflow instead. This is where it fits better and where you will get more answers (and not a closed question). Thanks. – kaiser Mar 8 '16 at 3:02
0

You need to call global $variable; before you can use the variable. Try the code below instead for that line.

// setup your global scope here

global $icon_var;

// set the new value in global scope

$icon_var = get_post_meta($menu_item->ID, '_menu_item_custom', true);  

// then use the variable

$menu_list .= "\t\t\t\t\t<li><a href=\"$url\">$icon_var<span>$title</span></a></li>\n";

DYNAMIC GLOBAL VARIABLE NAME

If you Created your data in a global variable and now you want to access it with a dynamic name reference then use the $GLOBALS directly.

// 'icon_home' set as global

global $icon_home;
$icon_home = '<svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fill="#000000" d="M10,20V14H14V20H19V12H22L12,3L2,12H5V20H10Z"></path></svg>';

// variable name retrieved dynamically

// 'icon_home';
$icon_var = get_post_meta($menu_item->ID, '_menu_item_custom', true); 
$icon = $GLOBALS [$icon_var]; 

// output icon

echo "\t\t\t\t\t<li><a href=\"$url\">$icon<span>$title</span></a></li>\n";
| improve this answer | |
  • It just echoes the _menu_custom_item value instead of <?php global $icon_home; echo $icon_home; ?>. Like this: <a href="http://slembas.esy.es/"> $icon_home <span>Главная</span> </a> – SLembas Jan 28 '16 at 15:36
  • Yes, It need to be executed. – SLembas Jan 28 '16 at 15:51
  • Am I stupid or what... (I am stupid at PHP). Still doesn't work. Just shows the variable. To make it clear, that's how the compiled code should look: <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> – SLembas Jan 28 '16 at 16:08
  • And the only problem was commented <!--?php global $icon_home; echo $icon_home; ?-->. It just should be uncommented. – SLembas Jan 28 '16 at 16:11
  • Oh my! After experimenting with changing the code by your samples I finally got it to work! $icon = $GLOBALS [$icon_var]; helped. Thank you really much! – SLembas Jan 28 '16 at 17:06
0

Now it works. Im very grateful to jgraup!

All that I needed to change is to add $icon = $GLOBALS[$icon_var]; and change the $menu_list structure as jgraup recommended. Even there is no need for third-party changes.

The final code:

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);
        $icon = $GLOBALS[$icon_var];
        $title = $menu_item->title;
        $url = $menu_item->url;
        $menu_list.= "\t\t\t\t\t<li><a class=\"icon\" href=\"$url\">$icon<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;
}
| improve this answer | |

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