我想自动使任何父页面链接到它的第一个子页面,如果它有一个。像这样:
父级1(/\\subpage-1)子级1(/\\subpage-1)子级2(/\\subpage-2)子级3(/\\subpage-3)子级2(/\\subpage-2)子级1(/\\subpage-3)子级2(/父级2)子级3(/父级3)子级2(/父级2)子级3(/父级3)子级我找到了一些通过复杂的nav()助行器查找所有父级链接的代码。从这里我不知道如何找到第一个孩子的url。我想是的
$children_elements[0]->url
但事实似乎并非如此。
class Themeslug_Page_Navigation_Walker extends Walker_Nav_Menu {
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
$id_field = $this->db_fields[\'id\'];
if ( !empty( $children_elements[ $element->$id_field ] ) ) {
$element->url = "Where do I get the child URL?";
}
Walker_Nav_Menu::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
最合适的回答,由SO网友:Tom 整理而成
class Themeslug_Page_Navigation_Walker extends Walker_Nav_Menu {
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
$id_field = $this->db_fields[\'id\'];
if ( !empty( $children_elements[ $element->$id_field ] ) ) {
$element->url = $children_elements[$element->$id_field][0]->url;
}
Walker_Nav_Menu::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
}
}
这就成功了。