php - Wordpress walker call parent walk() method
When extending the base Walker class I need to extend thewalk()
method.
However, calling the parentwalk()
method yields no results.
These are the approaches I have tried:
public function walk($elements, $max_depth) {
parent::walk($elements, $max_depth);
}
public function walk($elements, $max_depth) {
$parent_class=get_parent_class($this);
$args = array($elements, $max_depth);
call_user_func_array(array($parent_class, 'walk'), $args);
}
It appears to me that as soon as I override thewalk()
things break.
Should this method return some specific value? Should I call the parent method differently?
Answer
Solution:
Walker::walk
will return the string resulting from the walk operation. What you will get is a text that has been created using the methodsWalker::display_element
,Walker::start_lvl
,Walker::start_el
and so on... What you will get from the parent method is already HTML code probably hard to modify in the right way in a second time, but if you really want to do that:Answer
Solution:
As pointed out in a comment by @TheFallen, the class Walker of Wordpress gives back an output
So, if you want to extend the class and overwrite the method, you MUST keep the original behaviour, returning the output too. My suggestion: