admin管理员组

文章数量:1410717

I have a menu that I created in Appearance -> Menus. One of it's items is the company phone number which is set in the theme customizer. I have a shortcode that will return that number:

/* shortcode to display phone number  */
function getPhone()
{
    return get_theme_mod( 'contact_phone_data');;
}

/* Add shortcode */
add_shortcode('contact_phone', 'getPhone');

Is there any way to use this in the Appearance -> Menus UI? when I enter [contact_phone] into the menu item's 'Navigation Label' field, it (as expected) displays the actual string rather than executing the shortcode.

Is there a way to indicate that the value of that field should be executed as a shortcode? If not, is there a standard way that this type of thing is handled in WP?

I have a menu that I created in Appearance -> Menus. One of it's items is the company phone number which is set in the theme customizer. I have a shortcode that will return that number:

/* shortcode to display phone number  */
function getPhone()
{
    return get_theme_mod( 'contact_phone_data');;
}

/* Add shortcode */
add_shortcode('contact_phone', 'getPhone');

Is there any way to use this in the Appearance -> Menus UI? when I enter [contact_phone] into the menu item's 'Navigation Label' field, it (as expected) displays the actual string rather than executing the shortcode.

Is there a way to indicate that the value of that field should be executed as a shortcode? If not, is there a standard way that this type of thing is handled in WP?

Share Improve this question asked Nov 11, 2019 at 16:00 Daveh0Daveh0 1912 silver badges13 bronze badges 2
  • the admin page "menu" is more for editing menu elements. then if you want to add an information next to the menu, it's better to add it in a theme file. – Kaperto Commented Nov 11, 2019 at 16:32
  • .Sorry, I'm not quite sure what you mean by a "theme file"? The phone number itself actually IS the menu item. In my case, it's the last item in the menu, so I guess I can take it out and add it right next to the menu in the template file, but what if it had to go in the middle of the menu? Same with the hyperlink on the menu item... how can one go about accessing that number from the theme settings so that it will update accordingly when the user sets/changes that value in the theme customizer? – Daveh0 Commented Nov 11, 2019 at 16:51
Add a comment  | 

1 Answer 1

Reset to default 0

Someone posted a similar question in the Advanced WordPress facebook group. I suggested adding some token text to the menu item's url and then swapping it out during render.

add_filter( 'nav_menu_link_attributes', 'dcwd_nav_menu_link_attributes', 10, 4 ); 
function dcwd_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
    // If PHONE_NUMBER is found then change it.
    if ( false !== strpos( $atts[ 'href' ], 'PHONE_NUMBER' ) ) {
        $atts[ 'href' ] = getPhone();
    }
    return $atts;
}

I also posted it to pastebin

本文标签: use value returned from shortcode as menu item