Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #114905
    ebetech
    Participant

    Greetings! Great theme. Once the client approves the look for their website, we will be purchasing the Pro version.

    In the meantime, I have been doing prep work. The client requests we keep the original top-navigation. So, I created a child theme, duplicated what was necessary, and replaced the Primary Menu code with a “require_once” statement. Problem is the child theme does not override the parent theme.

    I have tried the usual techniques (e.g., pluggable functions, function priority, and removing original hooks). None of them work. I tried different variations, like priority numbers, hook callouts (e.g., wp_loaded, init, clean_education_header, clean_education_after_header, etc.). Still no luck.

    Here’s the parent and child theme code I am working with. Some help is greatly appreciated. Thanks in advance.

    Parent Theme:

    
    if ( ! function_exists( 'clean_education_primary_menu' ) ) :
        function clean_education_primary_menu() {
            ?>
            <button id="menu-toggle-primary" class="menu-toggle"><?php esc_html_e( 'Menu', 'clean-education' ); ?></button>
    
            <div id="site-header-menu-primary" class="site-header-menu">
                    <nav id="site-navigation-primary" class="main-navigation" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'clean-education' ); ?>">
                    <h3 class="screen-reader-text"><?php _e( 'Primary menu', 'clean-education' ); ?></h3>
                        <?php
                            if ( has_nav_menu( 'primary' ) ) {
                                $args = array(
                                    'theme_location'    => 'primary',
                                    'menu_class'        => 'menu primary-menu',
                                    'container'         => false
                                );
                                wp_nav_menu( $args );
                            }
                            else {
                                wp_page_menu( array( 'menu_class'  => 'default-page-menu' ) );
                            }
                        ?>
                    </nav><!-- .main-navigation -->
            </div><!-- .site-header-menu -->
        <?php
        }
    endif;
    add_action( 'clean_education_header', 'clean_education_primary_menu', 60 );
    

    Child Theme:

    
        function clean_education_primary_menu() {
            ?>
            <button id="menu-toggle-primary" class="menu-toggle"><?php esc_html_e( 'Menu', 'clean-education' ); ?></button>
    
            <div id="site-header-menu-primary" class="site-header-menu">
                    <nav id="site-navigation-primary" class="main-navigation" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'clean-education' ); ?>">
                    <h3 class="screen-reader-text"><?php _e( 'Primary menu', 'clean-education' ); ?></h3>
                        <?php require_once('topnav.php'); ?>
                    </nav><!-- .main-navigation -->
            </div><!-- .site-header-menu -->
        <?php
        }
    add_action( 'clean_education_header', 'clean_education_primary_menu', 65 );
    function child_remove_parent_function() {
        remove_action( 'clean_education_header', 'clean_education_primary_menu', 60 );
    }
    add_action( 'wp_loaded', 'child_remove_parent_function' );
    
    #114914
    Pratik
    Participant

    Hi @ebtechj,

    Try following code and let me know how it goes:

    
    function clean_education_primary_menu() {
            ?>
            <button id="menu-toggle-primary" class="menu-toggle"><?php esc_html_e( 'Menu', 'clean-education' ); ?></button>
    
            <div id="site-header-menu-primary" class="site-header-menu">
                    <nav id="site-navigation-primary" class="main-navigation" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'clean-education' ); ?>">
                    <h3 class="screen-reader-text"><?php _e( 'Primary menu', 'clean-education' ); ?></h3>
                        <?php require_once('topnav.php'); ?>
                    </nav><!-- .main-navigation -->
            </div><!-- .site-header-menu -->
        <?php
        }
    function clean_education_child_menu_override() {
        remove_action( 'clean_education_header', 'clean_education_primary_menu', 60 );
        add_action( 'clean_education_header', 'clean_education_primary_menu', 65 );
    }
    add_action( 'init', 'clean_education_child_menu_override' );
    

    Regards,
    Pratik

    #115057
    ebetech
    Participant

    Awesome, that worked. Why didn’t I see that? Also, I forgot to include the new child file in the functions.php file.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Override Primary Menu via Child Theme’ is closed to new replies.