dtq26360 2013-12-19 22:57
浏览 199
已采纳

我想在我的Wordpress菜单中加入一个高级自定义字段

Here's my code now:

$menu_icon = get_field('menu_icon');

if($menu_icon) {
    $m_icon = '<img src="'.the_field('menu_icon').'">';
} else {
    $m_icon = "";
};

$personal = array(
    'theme_location'  => 'personal',
    'menu'            => 'Personal Menu',
    'before'          => '$m_icon',
);

wp_nav_menu( $personal );

And here's what it spits out:

<ul id="menu-personal-menu" class="menu">
    <li class="menu-item>
        $m_icon <a href="#">Link</a>
        <ul class="sub-menu">
            <li>$m_icon <a href="#1">SubLink 1</a></li>
            <li>$m_icon <a href="#2">SubLink 2</a></li>
            <li>$m_icon <a href="#3">SubLink 3</a></li>
        </ul>
    </li>
</ul>

I'd like to have it produce something like this:

<ul id="menu-personal-menu" class="menu">
    <li class="menu-item>
        <a href="#">Link</a>
        <ul class="sub-menu">
            <li><img src="path/sublinkimg1"> <a href="#1">SubLink 1</a></li>
            <li><img src="path/sublinkimg2"> <a href="#2">SubLink 2</a></li>
            <li><img src="path/sublinkimg3"> <a href="#3">SubLink 3</a></li>
        </ul>
    </li>
</ul>

Here's the general idea: enter image description here

It's wrong for a few reasons. I know the advanced custom field: "menu_icon" probably isn't able to retrieve anything outside of a loop, and I'm not familiar enough with the wp_nav_menu() function to know whether or not it's possible to echo an image specific to the page it's displaying.

Does anyone know how I can achieve this?

  • 写回答

2条回答 默认 最新

  • dtv995719 2014-01-03 17:52
    关注

    One thing I notice right away is that you have extra single quotes, and php is translating the content literally. You actually want the string contained in $m_icon, and NOT the string '$m_icon' which is not a variable.

    $personal = array(
        'theme_location'  => 'personal',
        'menu'            => 'Personal Menu',
        'before'          => $m_icon, //this line was wrong, single quotes means 'literally this$ $tring'
    );
    

    Edit: I took the liberty of writing a filter function for what you're trying to do. Cheers.

    function insert_icons($items, $menu, $args){
        //check our menu name so we filter the right one
        if($menu->name=='Personal Menu'){
            //loop through the menu items
            foreach($items as $key => $item){
                //make sure object_id is set (this is the page or post id)
                if(isset($item->object_id) && !empty($item->object_id)){
                    //grab the menu icon
                    $menu_icon = get_field('menu_icon',$item->object_id);
                    //make sure the previous line returned something
                    if(isset($menu_icon) && !empty($menu_icon)){
                        //create our image string
                        $m_icon = '<img src="'.$menu_icon.'">';
                        //insert it into the page title
                        $item->title = $m_icon.$item->title;
                    }
                }
            }
        }
        return $items;
    }
    add_filter('wp_get_nav_menu_items','insert_icons',NULL,3);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题