1

我发现这个可爱的插件会自动将菜单项添加到您的页面自定义菜单中。我需要做的就是调整它以适用于分层自定义帖子类型。我想这对你们中的一些人来说会很容易......希望:) 在此先感谢。

这里是代码:

class AutoSubmenu {

/**
 * Constructor
 */
function __construct() {
    add_action( 'publish_page', array( &$this, 'on_publish_page' ) );
}

/**
 * When publishing a new child page, add it to the appropriate custom menu.
 */
function on_publish_page( $post_id ) {

    // Theme supports custom menus?
    if ( ! current_theme_supports( 'menus' ) ) {
        return;
    }

    // Published page has parent?
    $post = get_post( $post_id );
    if ( ! $post->post_parent ) {
        return;
    }

    // Get menus with auto_add enabled
    $auto_add = get_option( 'nav_menu_options' );
    if ( empty( $auto_add ) || ! is_array( $auto_add ) || ! isset( $auto_add['auto_add'] ) ) {
        return;
    }
    $auto_add = $auto_add['auto_add'];
    if ( empty( $auto_add ) || ! is_array( $auto_add ) ) {
        return;
    }

    // Loop through the menus to find page parent
    foreach ( $auto_add as $menu_id ) {
        $menu_parent = NULL;
        $menu_items = wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft') );
        if ( ! is_array( $menu_items ) ) {
            continue;
        }
        foreach ( $menu_items as $menu_item ) {
            // Item already in menu?
            if ( $menu_item->object_id == $post->ID ) {
                continue 2;
            }
            if ( $menu_item->object_id == $post->post_parent ) {
                $menu_parent = $menu_item;
            }
        }
        // Add new item
        if ( $menu_parent ) {
            wp_update_nav_menu_item( $menu_id, 0, array(
                'menu-item-object-id' => $post->ID,
                'menu-item-object' => $post->post_type,
                'menu-item-parent-id' => $menu_parent->ID,
                'menu-item-type' => 'post_type', /*array( 'post_type' => 'bio_diversity')*/
                'menu-item-status' => 'publish'
            ) );
        }
    }
}

}

$auto_submenu = new AutoSubmenu();
4

0 回答 0