【问题标题】:Wordpress Header.php: Creating Multiple Menus for Separate PagesWordpress Header.php:为单独的页面创建多个菜单
【发布时间】:2015-02-22 21:13:07
【问题描述】:

我正在尝试为 Wordpress 网站上的单独页面创建多个菜单。我希望主题加载主菜单,除非个别页面通过自定义字段指定不同的页面。

尝试将此代码添加到 Header.php,但主题自定义设置存在问题,因为 php 没有“wp_nav_menu”。

<?php wp_nav_menu( array( 'container' => 'none', 'container_class' => 'menu-header', 'theme_location' => 'primary', 'menu' => get_post_meta( $post->ID, 'MenuName', true) ) ); ?>

当我将它直接添加到当前代码时,它会导致主菜单在主题中已经存在的自定义菜单之上加倍。以下是主题 header.php 现有代码:

<?php do_action( 'presscore_body_top' ); ?>

<div id="page"<?php if ( 'boxed' == of_get_option('general-layout', 'wide') ) echo ' class="boxed"'; ?>>

 <?php if ( of_get_option('top_bar-show', 1) ) : ?>

<?php get_template_part( 'templates/header/top-bar', of_get_option('top_bar-content_alignment', 'side') ); ?>

<?php endif; // show top bar ?>

<?php if ( apply_filters( 'presscore_show_header', true ) ) : ?>

<?php get_template_part( 'templates/header/header', of_get_option( 'header-layout', 'left' ) ); ?>

<?php endif; // show header ?>

<?php do_action( 'presscore_before_main_container' ) ; ?>

<div id="main" <?php presscore_main_container_classes(); ?>><!-- class="sidebar-none", class="sidebar-left", class="sidebar-right" -->

 <?php if ( presscore_is_content_visible() ): ?>

    <div class="main-gradient"></div>

    <div class="wf-wrap">
        <div class="wf-container-main">

            <?php do_action( 'presscore_before_content' ); ?>

<?php endif; ?>

我可以在这段代码中的哪个位置为单个页面创建此选项以获取单独的菜单?

【问题讨论】:

    标签: php wordpress menu header-files


    【解决方案1】:

    不要使用menu 数组键。相反,请为您的菜单使用theme_location 键。 (您可以完全排除 menu。)类似:

    wp_nav_menu( array(
      'container' => false,
      'theme_location' => get_post_meta( $post->ID, 'MenuName', true) ? get_post_meta( $post->ID, 'MenuName', true) : 'primary'
    ) );
    

    如果没有自定义菜单,它将回退到“主要”。旁注:如果您不指定容器,container_class 将无用,因此请使用容器或删除 container_class

    【讨论】:

    • 我的问题是了解在现有 header.php 中放置此代码的位置。当我将它放在代码中时,它会导致第二个菜单加载到现有的顶部。如何在不加载重复菜单的情况下获取当前代码来注册此功能?感谢您抽出宝贵时间提供帮助。
    猜你喜欢
    • 2016-04-16
    • 1970-01-01
    • 2012-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    相关资源
    最近更新 更多