简体   繁体   中英

Wordpress insert shortcode into menu item

I have such a problem. In functions.php I have such code:

function Svg_Path($attr) {
    $a = shortcode_atts(array(
        'path' => 'Some text'
    ), $attr);
    $svg = '<svg class="menu-item-icon"><use xlink:href=' . get_template_directory_uri(). '/img/svg/sprite.svg' . $a["path"] . '></use></svg>';
    return $svg;
}
add_shortcode( 'SvgPath', 'Svg_Path' );

Normally, I can use in pageBuilder this

<div>[SvgPath path='#logo__skype']</div>

And the result will be the displayed Skype Icon. But when I try to insert this shortcode into Appearence>Menu>Link text I get the shortcode as plain text

Where do I try to input my shortcode

So can you help me wwith this, so I can recieve skype logo from svg sprite in menu item

Can you not use the updated FontAwesome to create this and add this into your menu instead?

https://fontawesome.com/icons/skype?style=brands may help you out.

Try the following code. The code will parse your shortcode and show you the shortcode content. It'll only work on frontend.

add_filter( 'the_title', function( $title, $item_id ) {
    if ( 'nav_menu_item' === get_post_type( $item_id ) ) {
        return do_shortcode( $title );
    }
    return $title;
}, 10, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM