我应该如何使用Starter Content实用程序来显示一些默认菜单?

时间:2017-07-18 作者:user7482285

我最近开始研究WordPress主题,并发现了这个实用程序。我目前正在使用WordPress 4.7.4。我对代码做了以下修改,

在功能中。PHP;

function mytheme_setup(){
    register_nav_menus( array(
    \'side\'    => __( \'Side Menu\', \'mytheme\' ),
    \'social\' => __( \'Social Links Menu\', \'mytheme\' ),
    ) );

    $starter_content = array(
    // Set up nav menus for each of the two areas registered in the theme.
    \'nav_menu\'  =>  array(
        // Assign a menu to the "side" location.
        \'side\'  =>  array(
            \'name\'  =>  __( \'Side Menu\', \'mytheme\' ),
            \'items\' =>  array(
                \'link_recent\'   =>  array(
                    \'title\' =>  _x( \'Recent Articles\', \'Theme starter content\' ),
                    \'url\'   =>  site_url( \'/#recent-articles\' ),
                ),
                \'link_about\'    =>  array(
                    \'title\' =>  _x( \'About Us\', \'Theme starter content\' ),
                    \'url\'   =>  site_url( \'/#about-us\' ),
                ),
                \'link_contact\'  =>  array(
                    \'title\' =>  _x( \'Contact\', \'Theme starter content\' ),
                    \'url\'   =>  site_url( \'/#contact-us\' ),
                ),
            ),
        ),

        // Assign a menu to the "social" location.
        \'social\' => array(
            \'name\' => __( \'Social Links Menu\', \'mytheme\' ),
            \'items\' => array(
                \'link_facebook\',
                \'link_twitter\',
                \'link_instagram\',
                \'link_youtube\',
            ),
        ),
    ),
    );

    $starter_content = apply_filters( \'mytheme_starter_content\', $starter_content );

    add_theme_support(\'starter-content\', $starter_content);
}
add_action( \'after_setup_theme\', \'mytheme_setup\' );
而且,在头版。PHP,

    <?php if ( has_nav_menu( \'side\' ) ) : ?>
        <div class="navigation-side">

            <nav id="site-navigation" class="main-navigation" role="navigation">
            <?php
             wp_nav_menu( array(
                 \'theme_location\'    =>  \'side\',
             ));
             ?>
            </nav>

        </div><!-- .navigation-side -->
    <?php endif; ?>
但是,我仍然找不到任何关于我的主题的入门内容菜单。

1 个回复
SO网友:birgire

我想你这里有个拼写错误:

\'nav_menu\'  =>  array(
替换为:

\'nav_menus\'  =>  array(
我们还可以查看它是如何在默认的2017主题中设置的,并进一步了解它是如何在get_theme_starter_content():

// And nav menu items are grouped into nav menus.
case \'nav_menus\' :
    foreach ( $config[ $type ] as $nav_menu_location => $nav_menu ) {

        // Ensure nav menus get a name.
        if ( empty( $nav_menu[\'name\'] ) ) {
            $nav_menu[\'name\'] = $nav_menu_location;
        }

        $content[ $type ][ $nav_menu_location ][\'name\'] = $nav_menu[\'name\'];

        foreach ( $nav_menu[\'items\'] as $id => $nav_menu_item ) {
            if ( is_array( $nav_menu_item ) ) {

                // Item extends core content.
                if ( ! empty( $core_content[ $type ][ $id ] ) ) {
                    $nav_menu_item = array_merge( $core_content[ $type ][ $id ], $nav_menu_item );
                }

                $content[ $type ][ $nav_menu_location ][\'items\'][] = $nav_menu_item;
            } elseif ( is_string( $nav_menu_item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $nav_menu_item ] ) ) {
                $content[ $type ][ $nav_menu_location ][\'items\'][] = $core_content[ $type ][ $nav_menu_item ];
            }
        }
    }
    break;

结束

相关推荐

Problem with custom menus

在菜单上,我说的是自定义菜单,我有一个带有几列的页脚,每列都有一个项目列表,每列菜单都需要从wp admin上的菜单选项进行更新,所以我创建了关于函数的部分。调用菜单并在页脚上添加每个菜单的php。php文档,类似于:<?php wp_nav_menu( array(\'menu\' => \'f-1\', \'menu_class\' => \'\', \'container\' => \'nav\' )); ?> <?php wp_nav_menu( arr