首页    >    新闻资讯    >   给每个页面都设置不同的菜单

给每个页面都设置不同的菜单

做项目总是有一些很奇怪的要求,比如有些客户需要给每个页面设置不同的菜单,但是这些总是难不倒 WordPress:

<?php
/*
Plugin Name: WPJAM 自定义菜单

Description: 实现每个页面都设置不同的菜单
Version: 0.1
Author: Denis

*/

add_action('init','wpjam_register_nav_menus_init');
function wpjam_register_nav_menus_init(){

    global $wpdb;
    $sql = "SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = 'nav_menu';";
    $nav_menus = $wpdb->get_col($sql);

    if($nav_menus){
        foreach ($nav_menus as $nav_menu){
            if($nav_menu){
                register_nav_menu( $nav_menu, $nav_menu);
            }
        }
    }
}

add_action('save_post', 'wpjam_save_nav_menu_options');
function wpjam_save_nav_menu_options($post_id){    
    if ( defined('DOING_AUTOSAVE') &#038;&#038; DOING_AUTOSAVE )
        return $post_id;
    update_post_meta($post_id, 'nav_menu',$_POST['nav_menu']);
}

function wpjam_nav_menu_options(){
    global $post;
    if($_REQUEST['nav_menu']){
        $nav_menu = $_REQUEST['nav_menu'];
    }else{
        $nav_menu = get_post_meta($post->ID, 'nav_menu', true);
    }
?>
    <p><input type="text" name="nav_menu" value="<?php echo $nav_menu?>" id="nav_menu" style="width:98%;"  /></p>
<?php
}

add_action('admin_menu', 'wpjam_nav_menu_options_box');
function wpjam_nav_menu_options_box() {
    add_meta_box('nav_menu_options', '自定义菜单位置', 'wpjam_nav_menu_options', 'page', 'normal', 'high');  
}

将上面代码作为一个插件上传激活,然后在每个页面下面就有一个自定义菜单位置的输入框,只要设置了名称,就会在菜单管理界面出现菜单位置,然后你创建一个新的菜单,并设置给菜单位置即可。
 
如果你想实现每篇日志也可以自定义菜单位置,在 wpjam_nav_menu_options_box 添加下面一行:

add_meta_box('nav_menu_options', '自定义菜单位置', 'wpjam_nav_menu_options', 'post', 'normal', 'high');

作者:DENIS

来源:http://blog.wpjam.com/m/different-menu-for-different-page/

分类:新闻资讯

标签:,

* 版权声明:作者WordPress啦! 转载请注明出处。

kadıköy escortbostancı escortataşehir escortbostancı escortmaltepe escortmaltepe escortanadolu yakası escortanadolu yakası escortataşehir escortbostancı escortistanbul escortkartal escortbostancı escortkartal escortkadıköy escortpendik escortpendik escortmaltepe escortataşehir escortmaltepe escortmaltepe escortanadolu yakası escortkadıköy escortataşehir escortkadıköy escortataşehir escortümraniye escortataşehir escortkartal escortescort bayankartal escortpendik escortkartal escortgöztepe escorttuzla escortkadıköy escortkartal escortkadıköy escortmaltepe escortkartal escortataşehir escortmaltepe escortbostancı escortkartal escortpendik escortkadıköy escortataköy escortavrupa yakası escortataköy escortbakırköy escortbeylikdüzü escorttaksim escorthalkalı escortetiler escortbodrum escortmalatya escortankara escortantalya escortmersin escortantalya escortHacklinkHacklinkshellwso shellwso shellgörüntülü sohbettipobetbahis siteleri