0

I'm working in a child theme and I need to override a function in order to remove some unwanted items on a top menu. So I have research a bit and found a lot of docs, really, like this, this and this among others but still not clear at all to me how to achieve this on my specific case. So I hope someone here can point me on the right direction. I have this functions at original theme functions.php file:

function udesign_nav() {
    if ( function_exists( 'wp_nav_menu' ) ) {
        $defaults = array(
            'container_class' => 'navigation-menu',
            'container_id' => 'navigation-menu',
            'menu_id'    => 'main-top-menu',
            'menu_class' => 'sf-menu',
            'link_before'=> '<span>',
            'link_after' => '</span>',
            'theme_location' => 'primary',
            'fallback_cb' => 'udesign_nav_fallback'
        );
        wp_nav_menu( $defaults );
    } else {
        udesign_nav_fallback();
    }
}

function udesign_nav_fallback() {
    $menu_html = '<div id="navigation-menu" class="navigation-menu">';
    $menu_html .= '<ul id="main-top-menu" class="sf-menu">';
    $menu_html .= is_front_page() ? "<li class='current_page_item'>" : "<li>";
    $menu_html .= '<a href="'.home_url().'"><span>'.esc_html__('Home', 'udesign').'</span></a></li>';
    $menu_html .= wp_list_pages('depth=5&title_li=0&sort_column=menu_order&link_before=<span>&link_after=</span>&echo=0');
    $menu_html .= '</ul>';
    $menu_html .= '</div>';
    echo $menu_html;
}

And it is called, I think, here:

// Setup the main menu
function udesign_top_main_menu() {
    ob_start(); ?>
            <div class="clear"></div>
            <div id="main-menu">
                <div id="dropdown-holder" class="container_24">
<?php               udesign_nav(); // this function calls the main menu ?>
                </div>
                <!-- end dropdown-holder -->
            </div>
            <!-- end top-main-menu -->
<?php
    $main_menu_html = ob_get_clean();
    echo apply_filters( 'udesign_get_top_main_menu', $main_menu_html );
}
add_action('udesign_top_wrapper_bottom', 'udesign_top_main_menu', 10);

What I need to override is just remove this two lines from udesign_nav_fallback() function:

$menu_html .= is_front_page() ? "<li class='current_page_item'>" : "<li>";
$menu_html .= '<a href="'.home_url().'"><span>'.esc_html__('Home', 'udesign').'</span></a></li>';

What I need to do in order to achieve that using the functions.php on the child theme?

1

You can't override the function(s). They aren't meant to be overwritten. See: "Override parent theme function that is not hooked or in the functions.php file" and "Is it possible to disable a function of a parent theme?". That is the direct answer to your question.

You can, however, almost certainly achieve the result you want by using the filter provided by the theme-- udesign_get_top_main_menu-- or any number of filters provided by wp_nav_menu() or by the Walker_Nav_menu class-- perhaps wp_nav_menu_objects or wp_nav_menu_items.

Per the question, you are wanting to remove:

$menu_html .= is_front_page() ? "<li class='current_page_item'>" : "<li>";
$menu_html .= '<a href="'.home_url().'"><span>'.esc_html__('Home', 'udesign').'</span></a></li>';

Ok. You probably need to use the filter provided in the code posted:

function udesign_strip($menu) {
  var_dump($menu);
  $menu_html = is_front_page() ? "<li class='current_page_item'>" : "<li>";
  $menu_html .= '<a href="'.home_url().'"><span>'.esc_html__('Home', 'udesign').'</span></a></li>';
  $menu = str_replace($menu_html,'',$menu);
  return $menu;
}
add_filter('udesign_get_top_main_menu','udesign_strip');

I don't like str_replaceing markup but that is probably the best way in this case.

  • I am confused at this moment, can you show me what you talked previous with some code? Will be easy for me understand that way – ReynierPM Jun 29 '15 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.