在WordPress主题中按代码创建导航菜单

时间:2015-12-13 09:19:47

标签: php wordpress

我基本上来自.NET和学习woocommerce主题..我在主页面的Nav菜单中找到了此代码段

 <?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>

编辑:

我在/ httpdocs / ECOM / wp-includes / nav-menu.php中找到导航菜单代码

function wp_create_nav_menu( $menu_name ) {

    return wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
}

和这一个

function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
    $menu_id = (int) $menu_id;

    $_menu = wp_get_nav_menu_object( $menu_id );

    $args = array(
        'description' => ( isset( $menu_data['description'] ) ? $menu_data['description']  : '' ),
        'name'        => ( isset( $menu_data['menu-name']   ) ? $menu_data['menu-name']    : '' ),
        'parent'      => ( isset( $menu_data['parent']      ) ? (int) $menu_data['parent'] : 0  ),
        'slug'        => null,
    );

    // double-check that we're not going to have one menu take the name of another
    $_possible_existing = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );
    if (
        $_possible_existing &&
        ! is_wp_error( $_possible_existing ) &&
        isset( $_possible_existing->term_id ) &&
        $_possible_existing->term_id != $menu_id
    ) {
        return new WP_Error( 'menu_exists',
            /* translators: %s: menu name */
            sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
                '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
            )
        );
    }

    // menu doesn't already exist, so create a new menu
    if ( ! $_menu || is_wp_error( $_menu ) ) {
        $menu_exists = get_term_by( 'name', $menu_data['menu-name'], 'nav_menu' );

        if ( $menu_exists ) {
            return new WP_Error( 'menu_exists',
                /* translators: %s: menu name */
                sprintf( __( 'The menu name %s conflicts with another menu name. Please try another.' ),
                    '<strong>' . esc_html( $menu_data['menu-name'] ) . '</strong>'
                )
            );
        }

        $_menu = wp_insert_term( $menu_data['menu-name'], 'nav_menu', $args );

        if ( is_wp_error( $_menu ) )
            return $_menu;

        do_action( 'wp_create_nav_menu', $_menu['term_id'], $menu_data );

        return (int) $_menu['term_id'];
    }

    if ( ! $_menu || ! isset( $_menu->term_id ) )
        return 0;

    $menu_id = (int) $_menu->term_id;

    $update_response = wp_update_term( $menu_id, 'nav_menu', $args );

    if ( is_wp_error( $update_response ) )
        return $update_response;

    $menu_id = (int) $update_response['term_id'];
    do_action( 'wp_update_nav_menu', $menu_id, $menu_data );
    return $menu_id;
}

我搜索了一些帖子并写了一些代码来更新菜单

 wp_update_nav_menu_object($menu_id, 0, array(
        'menu-item-title' =>  __('Home'),
        'menu-item-classes' => 'home',
        'menu-item-url' => home_url( '/' ), 
        'menu-item-status' => 'publish'));

    wp_update_nav_menu_object($menu_id, 0, array(
        'menu-item-title' =>  __('Activity'),
        'menu-item-classes' => 'activity',
        'menu-item-url' => home_url( '/activity/' ), 
        'menu-item-status' => 'publish'));

    wp_update_nav_menu_object($menu_id, 0, array(
        'menu-item-title' =>  __('Members'),
        'menu-item-classes' => 'members',
        'menu-item-url' => home_url( '/members/' ), 
        'menu-item-status' => 'publish'));

但这不起作用..我在这里缺少什么?

0 个答案:

没有答案