Basically, you add a custom walker to wp_nav_menu
. Whenever an item begins, you can check for the content of the page underlaying the navigation item.
When a content is available, read the content into a new DOMDocument
-Object and filter out all titles. With the titles, create sub-links to the current link beeing made in the start_el
-function:
Working example:
class Custom_Walker extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= '<li>';
// link attributes for the "normal" link
$attributes="";
$attributes .= ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$output .= sprintf( '%1$s<a%2$s>%3$s</a>%4$s',
$args->before,
$attributes,
apply_filters( 'the_title', $item->title, $item->ID ),
$args->after
);
// Check for titles in the content and add them as a sub-ul after the current a-element
// but only on level 3
if ( $depth == 3 ) {
$current_post = get_post( $item->object_id );
if ( $current_post->post_type == 'page' ) {
$doc = new DOMDocument;
$doc->LoadHTML( $current_post->post_content );
$titles = $doc->getElementsByTagName( 'h1' );
// begin the sub-list here
$output .= '<ul>';
foreach ( $titles as $a_title ) {
// clean up the title to use as a fragment in the href-attribute
$sanitized_title = sanitize_title( $doc->saveHTML( $a_title ) );
// and add the link to the output as a li-element
$output .= sprintf( '<li><a href="%s#%s">%s</a></li>', esc_attr( $item->url ), $sanitized_title, $doc->saveHTML( $a_title ) );
}
// end of sub-list
$output .= '</ul>';
$doc = null;
}
}
}
function end_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= '</li>';
}
}