自定义Wordpress菜单替换当前的标题菜单

时间:2019-04-12 09:34:48

标签: wordpress-theming

我正在尝试向Wordpress主题添加其他自定义菜单。我已经在menu.php文件中注册了菜单和位置,创建了一个新的页面模板并添加了菜单。但是,每当我向菜单添加项目时,它都会替换主标题中的所有内容,而且还会将其添加到我需要的位置。

任何帮助将不胜感激。

谢谢

杰克

  

下面是我的functions.php文件。我要添加的菜单是正确的   在底部称为“关于菜单”

        <?php
        function cc_mime_types( $mimes ){
            $mimes['svg'] = 'image/svg+xml';
            return $mimes;
        }
        add_filter( 'upload_mimes', 'cc_mime_types' );
        ?>
        <?php add_theme_support( 'post-thumbnails' ); ?>
        <?php
        add_filter('next_posts_link_attributes', 'posts_link_next');
        add_filter('previous_posts_link_attributes', 'posts_link_prev');

        function posts_link_next() {
            return 'class="next-button"';
        }

        function posts_link_prev() {
            return 'class="prev-button"';
        }; ?>
        <?php add_filter('show_admin_bar', '__return_false'); ?>
        <?php
        /**
         * Register our sidebars and widgetized areas.
         *
         */
        function arphabet_widgets_init() {

            register_sidebar( array(
                'name' => 'index-sidebar',
                'id' => 'index-sidebar-widget',
                'before_widget' => '<div class="sidebar-widget">',
                'after_widget' => '</div>',
                'before_title' => '<h2 class="widget-sidebar">',
                'after_title' => '</h2>',
            ) );
            register_sidebar( array(
                'name' => 'announcement',
                'id' => 'announcement',
                'before_widget' => '<div class="announcement">',
                'after_widget' => '</div>',
                'before_title' => '<h2 class="announcement-title">',
                'after_title' => '</h2>',
            ) );

            register_sidebar( array(
                'name' => 'tweets',
                'id' => 'tweets',
                'before_widget' => '',
                'after_widget' => '',
                'before_title' => '',
                'after_title' => '',
            ) );

            register_sidebar( array(
                'name' => 'events',
                'id' => 'widget-events',
                'before_widget' => '',
                'after_widget' => '',
                'before_title' => '',
                'after_title' => '',
            ) );
        }
        add_action( 'widgets_init', 'arphabet_widgets_init' );

        /*
         * Alters event's archive titles
         */
        function tribe_alter_event_archive_titles ( $original_recipe_title, $depth ) {

            // Modify the titles here
            // Some of these include %1$s and %2$s, these will be replaced with relevant dates
            $title_upcoming =   'Upcoming Events'; // List View: Upcoming events
            $title_past =       'Past Events'; // List view: Past events
            $title_range =      'Events for %1$s - %2$s'; // List view: range of dates being viewed
            $title_month =      'Events for %1$s'; // Month View, %1$s = the name of the month
            $title_day =        'Events for %1$s'; // Day View, %1$s = the day
            $title_all =        'All events for %s'; // showing all recurrences of an event, %s = event title
            $title_week =       'Events for week of %s'; // Week view

            // Don't modify anything below this unless you know what it does
            global $wp_query;
            $tribe_ecp = Tribe__Events__Main::instance();
            $date_format = apply_filters( 'tribe_events_pro_page_title_date_format', tribe_get_date_format( true ) );

            // Default Title
            $title = $title_upcoming;

            // If there's a date selected in the tribe bar, show the date range of the currently showing events
            if ( isset( $_REQUEST['tribe-bar-date'] ) && $wp_query->have_posts() ) {

                if ( $wp_query->get( 'paged' ) > 1 ) {
                    // if we're on page 1, show the selected tribe-bar-date as the first date in the range
                    $first_event_date = tribe_get_start_date( $wp_query->posts[0], false );
                } else {
                    //otherwise show the start date of the first event in the results
                    $first_event_date = tribe_event_format_date( $_REQUEST['tribe-bar-date'], false );
                }

                $last_event_date = tribe_get_end_date( $wp_query->posts[ count( $wp_query->posts ) - 1 ], false );
                $title = sprintf( $title_range, $first_event_date, $last_event_date );
            } elseif ( tribe_is_past() ) {
                $title = $title_past;
            }

            // Month view title
            if ( tribe_is_month() ) {
                $title = sprintf(
                    $title_month,
                    date_i18n( tribe_get_option( 'monthAndYearFormat', 'F Y' ), strtotime( tribe_get_month_view_date() ) )
                );
            }

            // Day view title
            if ( tribe_is_day() ) {
                $title = sprintf(
                    $title_day,
                    date_i18n( tribe_get_date_format( true ), strtotime( $wp_query->get( 'start_date' ) ) )
                );
            }

            // All recurrences of an event
            if ( function_exists('tribe_is_showing_all') && tribe_is_showing_all() ) {
                $title = sprintf( $title_all, get_the_title() );
            }

            // Week view title
            if ( function_exists('tribe_is_week') && tribe_is_week() ) {
                $title = sprintf(
                    $title_week,
                    date_i18n( $date_format, strtotime( tribe_get_first_week_day( $wp_query->get( 'start_date' ) ) ) )
                );
            }

            if ( is_tax( $tribe_ecp->get_event_taxonomy() ) && $depth ) {
                $cat = get_queried_object();
                $title = '<a href="' . esc_url( tribe_get_events_link() ) . '">' . $title . '</a>';
                $title .= ' &#8250; ' . $cat->name;
            }

            return $title;
        }
        add_filter( 'tribe_get_events_title', 'tribe_alter_event_archive_titles', 11, 2 );
        ?>
        <?php
        add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);

        function ad_filter_menu($sorted_menu_objects, $args) {

            // check for the right menu to filter
            // here we check for the menu with name 'Posts Menu'
            // given that you call it in you theme with:
            //   wp_nav_menu( array( 'menu' => 'Posts Menu' ) );
            // if you call the menu using theme_location, eg:
            //   wp_nav_menu( array( 'theme_location' => 'top_manu' ) );
            // check for:
            //   if ($args->theme_location != 'top_menu')
            if ($args->menu != 'Parents Menu')
                return $sorted_menu_objects;


            // edit the menu objects
            foreach ($sorted_menu_objects as $menu_object) {
                // searching for menu items linking to posts or pages
                // can add as many post types to the array
                if ( in_array($menu_object->object, array('post', 'page', 'any_post_type')) ) {
                    // set the title to the post_thumbnail if available
                    // thumbnail size is the second parameter of get_the_post_thumbnail()
                    $menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'full') . '<span class="outer"><span>' . $menu_object->title . "</span></span>": $menu_object->title;

                }
            }

            return $sorted_menu_objects;
        };

        ?>

        <?php

        function register_my_menu() {
          register_nav_menu('about-menu',__( 'About Menu' ));
        }
        add_action( 'init', 'register_my_menu' );

        ?>
  

下一个代码段来自我要添加的页面模板   菜单。

<?php
/*
Template Name: About Us Page
*/
?>

<?php get_header();?>

<div class="main">
        <div class="page-top-banner" id="page-four-banner">
            <h1><span>About</span></h1>
        </div>


<div class="parental-inner">

   <?wp_nav_menu( array( 'theme_location' => 'about-menu', 'container_class' => 'about_menu_class' ) ); ?>


       </div>






       <?php get_footer();?>

1 个答案:

答案 0 :(得分:0)

从函数php中删除您的register_my_menu函数,并替换为以下内容:

删除此:

function register_my_menu() {
  register_nav_menu('about-menu',__( 'About Menu' ));
}
add_action( 'init', 'register_my_menu' );

替换为:

//nav
register_nav_menus(array(
  'main-menu' => __('Main Menu'),
  'about-menu' => __('About Menu'),
));

页面模板上的输出正确吗?