Order Admin sub-menu items?

时间:2011-09-19 作者:Wyck

我正在使用向CPT管理菜单添加项目add_submenu_page 这很好,但它们被添加到CPT选项后的子菜单底部。我希望能够将它们放在最上面,但我想这个问题也可以应用于订购所有基于管理员的子菜单项。

我所尝试的(不起作用,我尝试了几种变体),

function custom_menu_order($menu_ord) {
       if (!$menu_ord) return true;

       return array( 

         \'edit.php?post_type=page\' =>array(
                                      \'edit.php?post_type=note\',
                                      \'edit_pages\',
                                      \'notes\',
                                      )   
                    );
}

add_filter(\'custom_menu_order\', \'custom_menu_order\');
add_filter(\'menu_order\', \'custom_menu_order\');
这是因为menu_order 过滤器不考虑子菜单?

3 个回复
最合适的回答,由SO网友:Brian Fegter 整理而成

过滤器“custom\\u menu\\u order”将不会在菜单顺序上工作,因为在wp admin/includes/menu中应用\\u过滤器。php提供false作为过滤内容。您可以尝试将false更改为$菜单,过滤器将运行良好。

由于我们显然无法触及核心,以下是我如何让它工作的:

function custom_menu_order(){
    global $submenu;

    $find_page = \'edit.php\';
    $find_sub = \'Post Tags\';

    foreach($submenu as $page => $items):
        if($page == $find_page):
            foreach($items as $id => $meta):
                if($meta[0] == $find_sub):
                    $submenu[$find_page][0] = $meta;
                    unset ($submenu[$find_page][$id]);
                    ksort($submenu[$find_page]);
                endif;
            endforeach;
        endif;
    endforeach;
}
add_action(\'_admin_menu\', \'custom_menu_order\');

SO网友:StephanieQ

我知道我正在发布necro帖子,但我最近也遇到了同样的问题。请注意我用的是Rao的solution on SO 找到这个答案。

假设您的CPT被称为“注释”。

/**
 * @see https://codex.wordpress.org/Plugin_API/Filter_Reference/menu_order
 */
add_filter( \'custom_menu_order\', \'change_note_submenu_order\' );

/**
 * Change the submenu order for the Note CPT in the Admin panel
 * 
 * @param  [type] $menu_ord I don\'t think this is actually used for anything...
 * @return [type]           $menu_ord
 * @see https://stackoverflow.com/questions/18766477/wordpress-change-admin-submenu-order
 */
function change_note_submenu_order( $menu_ord ) {
  global $submenu;

  /* Uncomment the line below to see all menu orders */
  // echo \'<pre>\'.print_r($submenu,true).\'</pre>\';

  /**
   * NOTE: Original note submenu order should be:
   *  5 (note list),
   * 10 (Add new),
   * 15 (Categories),
   * 16 (Tags),
   * 17 (Your Custom Page added via add_submenu_page) */
  $arr = array();
  $arr[] = $submenu[\'edit.php?post_type=note\'][17]; // Custom menu page (Omit if unused)
  $arr[] = $submenu[\'edit.php?post_type=note\'][5];  // Note List
  $arr[] = $submenu[\'edit.php?post_type=note\'][10]; // Add New Note
  $arr[] = $submenu[\'edit.php?post_type=note\'][15]; // Categories
  $arr[] = $submenu[\'edit.php?post_type=note\'][16]; // Tags
  $submenu[\'edit.php?post_type=note\'] = $arr;

  return $menu_ord;
}
绝对要注释掉echo \'<pre>\'.print_r($submenu,true).\'</pre>\'; 如果你有麻烦。还请记住,您也可以在change_note_submenu_order 作用

$notes_list  = array("Notes", "edit_posts", "edit.php?post_type=notes");
$custom_page = array("Custom Menu Page Title", "Capability", "Menu Slug", "Page Title");
$new_note    = array("Add New", "edit_posts", "post-new.php?post_type=note");
$arr         = array($custom_page, $notes_list, $new_note);
$submenu[\'edit.php?post_type=note\'] = $arr;
希望这对别人有帮助!

SO网友:Camwyn

另一个更简单问题的necro。

我只是想将我的自定义链接移到底部(添加了关于如何使用它来解决原始问题的评论):

public function change_wc_submenu_order( $menu_order ) {
    global $submenu;

    foreach ( $submenu[\'woocommerce\'] as $index => $item ) {
        if (\'My Custom Submenu Title\' === $item[0] ) {
            $custom = $item;
            unset( $submenu[\'woocommerce\'][$index] );
            break;
        }
    }

    if ( ! empty( $custom ) ) {
        // If we want it at the beginning, use array_unshift() instead
        array_push( $submenu[\'woocommerce\'], $custom );
    }

    return $menu_order;
}

结束

相关推荐

display order of connections

是否有方法控制连接的显示顺序?我有一个Articles=>Books连接,这样在每篇文章中我都可以引用一些书。我想在文章的脚注中展示引用的书籍。目前,如果我创建三本书的连接,然后将它们显示在文章的底部,我将按相反的顺序获取它们。我能换一下吗?