nilssonc
nilssonc

Reputation: 29

How to override function in WordPress Parent theme from Child theme

The parent theme has the following:


public function mega_menu_attributes( $atts, $item, $args, $depth ) {

        // Get Mega Menu Type.
        $menu_type = $this->is_mega_menu( $item, $args );

        // Mega Menu attrs for terms.
        if ( in_array( $menu_type, array( 'term', 'child-term' ), true ) ) {
            $atts['data-term'] = $item->object_id;

            if ( 'term' === $menu_type ) {
                $atts['data-numberposts'] = 5;
            } elseif ( 'child-term' === $menu_type ) {
                $atts['data-numberposts'] = 6;
            }
        }

        // Mega Menu attrs for posts.
        if ( isset( $item->mega_menu_children ) && 'mixed' === $menu_type ) {
            $layout = $this->identify_mega_menu_layout( $item->mega_menu_children );

            if ( 'posts' === $layout ) {

                $posts = array();

                foreach ( $item->mega_menu_children as $_post ) {
                    $posts[] = $_post->object_id;
                }

                $atts['data-posts'] = implode( '|', $posts );

                $atts['data-numberposts'] = 5;
            }
        }

        return $atts;
    }

I want to override $atts['data-numberposts'] = 6; with $atts['data-numberposts'] = 12;

That's all I'm trying to do in my Child theme functions.php

I've checked out several threads here that cover similar cases, but none of the solutions I've tried from them have worked. This includes:

function remove_parent_theme_mega_menu_atts()
{
    remove_filter( 'nav_menu_link_attributes', array( $this, 'mega_menu_attributes' ), 10, 4 );
}

add_action('init', 'remove_parent_theme_mega_menu_atts');

I'm like intermediate with PHP/WordPress and thought I could get this one figured out, but I'm just in over my head now as I couldn't get any of these solutions to work.

Any help appreciated!

Upvotes: 0

Views: 498

Answers (1)

AaronJ
AaronJ

Reputation: 1170

It seems like this should work. What happens?

function test_run_before($atts, $item, $args) {
  $atts['data-numberposts'] = 12;
  return $atts;
}
function test_run_after($atts, $item, $args) {
  $atts['data-numberposts'] = 12;
  return $atts;
}

add_filter('nav_menu_link_attributes', 'test_run_before', 9, 3);
add_filter('nav_menu_link_attributes', 'test_run_after', 11, 3);

Upvotes: 2

Related Questions