-Plugin name: Submenu auto
-Description: Automatically list submenu entries
add_filter( 'wp_edit_nav_menu_walker', 'willy_wp_edit_nav_menu_walker' );
function willy_wp_edit_nav_menu_walker( $walker ) {
$walker = 'Willy_Walker_Nav_Menu_Edit';
Walker_Nav_Menu_Edit::start_el( $temp, $item, $depth, $args, $id );
+ // -> build input for sub elements types
+ $type = __( 'sous-éléments' );
+ if ( 'taxonomy' === $item->type && is_taxonomy_hierarchical( $item->object ) ) {
+ $type = vsprintf( '<select name="menu-item-autolist-type[%1$s]">
+ <option value="sub-elems">%2$s</option>
+ <option value="sub-terms" %4$s>%3$s</option>
+ __( 'sous-éléments' ),
+ selected( 'sub-terms', $item->autolist_type, false ),
+ // -> build input for sub elements number
$input = sprintf( '<input type="number" style="width:3em;" name="menu-item-autolist-number[%1$s]" step="1" min="-1" value="%2$s">',
is_int( $item->autolist_number ) && true === $item->autolist ? $item->autolist_number : get_option( 'posts_per_page' )
$re = '/(<p class="field-description[\S\s]*?<\/p>)/';
- $subst = '$1' . vsprintf( '<p class="field-autolist description"><label for="edit-menu-item-autolist-%1$s">
+ $subst = vsprintf( '$1<p class="field-autolist description"><label for="edit-menu-item-autolist-%1$s">
<input type="checkbox" id="edit-menu-item-autolist-%1$s" value="oui" name="menu-item-autolist[%1$s]"%2$s /> %3$s
checked( true, $item->autolist, false ),
sprintf( __( 'Lister automatiquement %1$s %2$s' ),
$output .= preg_replace( $re, $subst, $temp );
update_post_meta( $menu_item_db_id, 'menu-item-autolist', $value );
$value_num = $value ? intval( $_REQUEST['menu-item-autolist-number'][ $menu_item_db_id ] ) : false;
update_post_meta( $menu_item_db_id, 'menu-item-autolist-number', $value_num );
+ $value_type = $value ? esc_attr( $_REQUEST['menu-item-autolist-type'][ $menu_item_db_id ] ) : false;
+ update_post_meta( $menu_item_db_id, 'menu-item-autolist-type', $value_type );
add_filter( 'wp_setup_nav_menu_item', 'willy_wp_setup_nav_menu_item' );
function willy_wp_setup_nav_menu_item( $menu_item ) {
$menu_item->autolist = boolval( get_post_meta( $menu_item->ID, 'menu-item-autolist', true ) );
$menu_item->autolist_number = intval( get_post_meta( $menu_item->ID, 'menu-item-autolist-number', true ) );
+ $menu_item->autolist_type = get_post_meta( $menu_item->ID, 'menu-item-autolist-type', true );
if ( $item->autolist && is_int( $item->autolist_number ) && 0 !== $item->autolist_number ) {
case 'post_type_archive':
- $autolist = get_autolist_for_archive( array(
+ $autolist = willy_get_autolist_for_archive( array(
'post_type' => $item->object,
'number' => $item->autolist_number,
if ( $item->object_id === get_option( 'page_for_posts' ) ) {
- $autolist = get_autolist_for_archive( array(
+ $autolist = willy_get_autolist_for_archive( array(
'number' => $item->autolist_number,
'count' => count( $items ),
- $autolist = get_autolist_for_post_parent( array(
+ $autolist = willy_get_autolist_for_post_parent( array(
'post_type' => $item->object,
'post_parent' => $item->object_id,
'number' => $item->autolist_number,
- $autolist = get_autolist_for_taxonomy( array(
- 'taxonomy' => $item->object,
- 'term_id' => $item->object_id,
- 'number' => $item->autolist_number,
- 'count' => count( $items ),
+ if ( 'sub-terms' === $item->autolist_type ) {
+ $autolist = willy_get_autolist_for_taxonomy_terms( array(
+ 'taxonomy' => $item->object,
+ 'term_id' => $item->object_id,
+ 'number' => $item->autolist_number,
+ 'count' => count( $items ),
+ $autolist = willy_get_autolist_for_taxonomy( array(
+ 'taxonomy' => $item->object,
+ 'term_id' => $item->object_id,
+ 'number' => $item->autolist_number,
+ 'count' => count( $items ),
if ( ! empty( $autolist ) ) {
-function get_autolist_for_taxonomy( $args ) {
+function willy_get_autolist_for_taxonomy( $args ) {
$query = apply_filters( 'autolist_query', array(
'suppress_filters' => false,
- return get_autolist( $query, $args );
+ return willy_get_autolist( $query, $args );
-function get_autolist_for_post_parent( $args ) {
+function willy_get_autolist_for_taxonomy_terms( $args ) {
+ $query = apply_filters( 'autolist_query', array(
+ 'taxonomy' => $args['taxonomy'],
+ 'number' => $args['number'],
+ 'child_of' => $args['term_id'],
+ ), 'taxonomy_terms', $args );
+ return willy_get_autolist( $query, $args );
+function willy_get_autolist_for_post_parent( $args ) {
$query = apply_filters( 'autolist_query', array(
'suppress_filters' => false,
'post_type' => $args['post_type'],
'post_parent' => $args['post_parent'],
), 'post_parent', $args );
- return get_autolist( $query, $args );
+ return willy_get_autolist( $query, $args );
-function get_autolist_for_archive( $args ) {
+function willy_get_autolist_for_archive( $args ) {
$query = apply_filters( 'autolist_query', array(
'suppress_filters' => false,
'post_type' => $args['post_type'],
'posts_per_page' => $args['number'],
- return get_autolist( $query, $args );
+ return willy_get_autolist( $query, $args );
-function get_autolist( $query, $args ) {
- $autolist = get_posts( $query );
- $vars = array( 'parent' => $args['parent']->ID, 'count' => $args['count'] + 1 );
+function willy_get_autolist( $query, $args ) {
+ $autolist = isset( $args['type-req'] ) && 'terms' === $args['type-req'] ? get_terms( $query ) : get_posts( $query );
+ $vars = array( 'parent' => $args['parent']->ID, 'count' => $args['count'] + 1, 'term_q' => isset( $args['type-req'] ) && 'terms' === $args['type-req'] );
array_walk( $autolist, 'willy_format_as_menu_item', $vars );
$p->menu_item_parent = $vars['parent'];
- $p->type = 'post_type';
- $p->object_id = $p->ID;
- $p->url = get_permalink( $p );
- $p->title = get_the_title( $p );
+ if ( $vars['term_q'] ) {
+ $p->object = $p->taxonomy;
+ $p->object_id = $p->term_ID;
+ $p->url = get_term_link( $p->term_ID, $p->taxonomy );
+ $p->type = 'post_type';
+ $p->object_id = $p->ID;
+ $p->url = get_permalink( $p );
+ $p->title = get_the_title( $p );
$p->classes = array( 'sub-elem-auto');
$p->menu_order = $vars['count'] + $k;