如何在菜单管理管理屏幕中添加自定义Metabox?

时间:2010-10-11 作者:NetConstructor.com

虽然我非常感谢wordpress核心团队最终集成了本机菜单管理功能,但我对一些我想更改的关键元素感到失望。

我需要一种显示页面的方式,这种方式与显示分级类别的方式相同,而不是显示在列表中,并且我需要一种手动包括可以添加到菜单的链接的方式。

如何做到这一点?

提前感谢

UPDATED所以,以下是我想做的两件事。当前,如果您转到默认的wordpress“菜单管理”管理屏幕,您可以选择在左侧显示“页面”元框。这里的问题是,当您单击“查看全部”选项卡时,没有一个页面的顺序正确,并且如果适用,它们也没有缩进。

CURRENT DEFAULT LAYOUT:

CURRENT DEFAULT WP ORGANIZATION

DESIRED LAYOUT: (请注意,我只是缩进它们以显示父关系,这些项目也应根据其排序顺序进行排序)。

what i would like

此外,在这个列表中(或者如果更容易的话,在它自己的metabox中),我需要一些硬编码链接的方法,这些链接应该以复选框列表的形式显示,这样就可以单击元素并将其包含在菜单中。这样做的原因是,我宁愿从列表中选择这些元素,而不必使用“自定义链接”手动添加这些元素。

1 个回复
最合适的回答,由SO网友:Jan Fabry 整理而成

我成功地做到了这一点,但它是一个烂摊子。基本上,助行器应具有以下参数:

$this->db_fields[\'parent\'] = \'post_parent\';
$this->db_fields[\'id\'] = \'ID\';
但是,要实现这一点,您需要删除现有的metabox回调,复制它,更改一行以便获得额外的过滤器,然后将其放回原处。然后,您可以通过自己设置了这些参数的助行器。

add_filter( \'admin_head-nav-menus.php\', \'wpse2770_admin_head_nav_menus\' );
function wpse2770_admin_head_nav_menus()
{
    // Hijack "Pages" meta box callback with one that has an extra filter for the walker class
    $GLOBALS[\'wp_meta_boxes\'][\'nav-menus\'][\'side\'][\'default\'][\'add-page\'][\'callback\'] = \'wpse2770_wp_nav_menu_item_post_type_meta_box\';

    // Since Walker_Nav_Menu_Checklist is not always available, we create this class in this function (didn\'t even know that was possible...)
    class WPSE2770_Walker_Nav_Menu_Checklist extends Walker_Nav_Menu_Checklist
    {
        public function __construct()
        {
            $this->db_fields[\'parent\'] = \'post_parent\';
            $this->db_fields[\'id\'] = \'ID\';
        }
    }

}

add_filter( \'wp_nav_menu_item_post_type_meta_box_walker\', \'wpse2770_wp_nav_menu_item_post_type_meta_box_walker\', 10, 3 );
function wpse2770_wp_nav_menu_item_post_type_meta_box_walker( $walker, $post_type, $context )
{
    if ( \'page\' == $post_type && \'view-all\' == $context ) {
        $walker = \'WPSE2770_Walker_Nav_Menu_Checklist\';
    }
    return $walker;
}

function wpse2770_wp_nav_menu_item_post_type_meta_box( $object, $post_type ) {
    global $_nav_menu_placeholder, $nav_menu_selected_id;

    $post_type_name = $post_type[\'args\']->name;

    // paginate browsing for large numbers of post objects
    $per_page = 50;
    $pagenum = isset( $_REQUEST[$post_type_name . \'-tab\'] ) && isset( $_REQUEST[\'paged\'] ) ? absint( $_REQUEST[\'paged\'] ) : 1;
    $offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;

    $args = array(
        \'offset\' => $offset,
        \'order\' => \'ASC\',
        \'orderby\' => \'title\',
        \'posts_per_page\' => $per_page,
        \'post_type\' => $post_type_name,
        \'suppress_filters\' => true,
        \'update_post_term_cache\' => false,
        \'update_post_meta_cache\' => false
    );

    if ( isset( $post_type[\'args\']->_default_query ) )
        $args = array_merge($args, (array) $post_type[\'args\']->_default_query );

    // @todo transient caching of these results with proper invalidation on updating of a post of this type
    $get_posts = new WP_Query;
    $posts = $get_posts->query( $args );
    if ( ! $get_posts->post_count ) {
        echo \'<p>\' . __( \'No items.\' ) . \'</p>\';
        return;
    }

    $post_type_object = get_post_type_object($post_type_name);

    $num_pages = $get_posts->max_num_pages;

    $page_links = paginate_links( array(
        \'base\' => add_query_arg(
            array(
                $post_type_name . \'-tab\' => \'all\',
                \'paged\' => \'%#%\',
                \'item-type\' => \'post_type\',
                \'item-object\' => $post_type_name,
            )
        ),
        \'format\' => \'\',
        \'prev_text\' => __(\'&laquo;\'),
        \'next_text\' => __(\'&raquo;\'),
        \'total\' => $num_pages,
        \'current\' => $pagenum
    ));

    if ( !$posts )
        $error = \'<li id="error">\'. $post_type[\'args\']->labels->not_found .\'</li>\';

    $walker = \'Walker_Nav_Menu_Checklist\';

    $current_tab = \'most-recent\';
    if ( isset( $_REQUEST[$post_type_name . \'-tab\'] ) && in_array( $_REQUEST[$post_type_name . \'-tab\'], array(\'all\', \'search\') ) ) {
        $current_tab = $_REQUEST[$post_type_name . \'-tab\'];
    }

    if ( ! empty( $_REQUEST[\'quick-search-posttype-\' . $post_type_name] ) ) {
        $current_tab = \'search\';
    }

    $removed_args = array(
        \'action\',
        \'customlink-tab\',
        \'edit-menu-item\',
        \'menu-item\',
        \'page-tab\',
        \'_wpnonce\',
    );

    ?>
    <div id="posttype-<?php echo $post_type_name; ?>" class="posttypediv">
        <ul id="posttype-<?php echo $post_type_name; ?>-tabs" class="posttype-tabs add-menu-item-tabs">
            <li <?php echo ( \'most-recent\' == $current_tab ? \' class="tabs"\' : \'\' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . \'-tab\', \'most-recent\', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent"><?php _e(\'Most Recent\'); ?></a></li>
            <li <?php echo ( \'all\' == $current_tab ? \' class="tabs"\' : \'\' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . \'-tab\', \'all\', remove_query_arg($removed_args))); ?>#<?php echo $post_type_name; ?>-all"><?php _e(\'View All\'); ?></a></li>
            <li <?php echo ( \'search\' == $current_tab ? \' class="tabs"\' : \'\' ); ?>><a class="nav-tab-link" href="<?php if ( $nav_menu_selected_id ) echo esc_url(add_query_arg($post_type_name . \'-tab\', \'search\', remove_query_arg($removed_args))); ?>#tabs-panel-posttype-<?php echo $post_type_name; ?>-search"><?php _e(\'Search\'); ?></a></li>
        </ul>

        <div id="tabs-panel-posttype-<?php echo $post_type_name; ?>-most-recent" class="tabs-panel <?php
            echo ( \'most-recent\' == $current_tab ? \'tabs-panel-active\' : \'tabs-panel-inactive\' );
        ?>">
            <ul id="<?php echo $post_type_name; ?>checklist-most-recent" class="categorychecklist form-no-clear">
                <?php
                $recent_args = array_merge( $args, array( \'orderby\' => \'post_date\', \'order\' => \'DESC\', \'posts_per_page\' => 15 ) );
                $most_recent = $get_posts->query( $recent_args );
                $args[\'walker\'] = new $walker;
                echo walk_nav_menu_tree( array_map(\'wp_setup_nav_menu_item\', $most_recent), 0, (object) $args );
                ?>
            </ul>
        </div><!-- /.tabs-panel -->

        <div class="tabs-panel <?php
            echo ( \'search\' == $current_tab ? \'tabs-panel-active\' : \'tabs-panel-inactive\' );
        ?>" id="tabs-panel-posttype-<?php echo $post_type_name; ?>-search">
            <?php
            if ( isset( $_REQUEST[\'quick-search-posttype-\' . $post_type_name] ) ) {
                $searched = esc_attr( $_REQUEST[\'quick-search-posttype-\' . $post_type_name] );
                $search_results = get_posts( array( \'s\' => $searched, \'post_type\' => $post_type_name, \'fields\' => \'all\', \'order\' => \'DESC\', ) );
            } else {
                $searched = \'\';
                $search_results = array();
            }
            ?>
            <p class="quick-search-wrap">
                <input type="text" class="quick-search input-with-default-title" title="<?php esc_attr_e(\'Search\'); ?>" value="<?php echo $searched; ?>" name="quick-search-posttype-<?php echo $post_type_name; ?>" />
                <img class="waiting" src="<?php echo esc_url( admin_url( \'images/wpspin_light.gif\' ) ); ?>" alt="" />
                <?php submit_button( __( \'Search\' ), \'quick-search-submit button-secondary hide-if-js\', \'submit\', false ); ?>
            </p>

            <ul id="<?php echo $post_type_name; ?>-search-checklist" class="list:<?php echo $post_type_name?> categorychecklist form-no-clear">
            <?php if ( ! empty( $search_results ) && ! is_wp_error( $search_results ) ) : ?>
                <?php
                $args[\'walker\'] = new $walker;
                echo walk_nav_menu_tree( array_map(\'wp_setup_nav_menu_item\', $search_results), 0, (object) $args );
                ?>
            <?php elseif ( is_wp_error( $search_results ) ) : ?>
                <li><?php echo $search_results->get_error_message(); ?></li>
            <?php elseif ( ! empty( $searched ) ) : ?>
                <li><?php _e(\'No results found.\'); ?></li>
            <?php endif; ?>
            </ul>
        </div><!-- /.tabs-panel -->


        <div id="<?php echo $post_type_name; ?>-all" class="tabs-panel tabs-panel-view-all <?php
            echo ( \'all\' == $current_tab ? \'tabs-panel-active\' : \'tabs-panel-inactive\' );
        ?>">
            <?php if ( ! empty( $page_links ) ) : ?>
                <div class="add-menu-item-pagelinks">
                    <?php echo $page_links; ?>
                </div>
            <?php endif; ?>
            <ul id="<?php echo $post_type_name; ?>checklist" class="list:<?php echo $post_type_name?> categorychecklist form-no-clear">
                <?php
                // WPSE 2770: And this is the filter we want to add!
                $walker = apply_filters( \'wp_nav_menu_item_post_type_meta_box_walker\', $walker, $post_type_name, \'view-all\' );
                $args[\'walker\'] = new $walker;

                // if we\'re dealing with pages, let\'s put a checkbox for the front page at the top of the list
                if ( \'page\' == $post_type_name ) {
                    $front_page = \'page\' == get_option(\'show_on_front\') ? (int) get_option( \'page_on_front\' ) : 0;
                    if ( ! empty( $front_page ) ) {
                        $front_page_obj = get_post( $front_page );
                        $front_page_obj->_add_to_top = true;
                        $front_page_obj->label = sprintf( _x(\'Home: %s\', \'nav menu front page title\'), $front_page_obj->post_title );
                        array_unshift( $posts, $front_page_obj );
                    } else {
                        $_nav_menu_placeholder = ( 0 > $_nav_menu_placeholder ) ? intval($_nav_menu_placeholder) - 1 : -1;
                        array_unshift( $posts, (object) array(
                            \'_add_to_top\' => true,
                            \'ID\' => 0,
                            \'object_id\' => $_nav_menu_placeholder,
                            \'post_content\' => \'\',
                            \'post_excerpt\' => \'\',
                            \'post_title\' => _x(\'Home\', \'nav menu home label\'),
                            \'post_type\' => \'nav_menu_item\',
                            \'type\' => \'custom\',
                            \'url\' => home_url(\'/\'),
                        ) );
                    }
                }
$args[\'walker\']->db_fields[\'parent\'] = \'post_parent\';
                $checkbox_items = walk_nav_menu_tree( array_map(\'wp_setup_nav_menu_item\', $posts), 0, (object) $args );

                if ( \'all\' == $current_tab && ! empty( $_REQUEST[\'selectall\'] ) ) {
                    $checkbox_items = preg_replace(\'/(type=(.)checkbox(\\2))/\', \'$1 checked=$2checked$2\', $checkbox_items);

                }

                echo $checkbox_items;
                ?>
            </ul>
            <?php if ( ! empty( $page_links ) ) : ?>
                <div class="add-menu-item-pagelinks">
                    <?php echo $page_links; ?>
                </div>
            <?php endif; ?>
        </div><!-- /.tabs-panel -->


        <p class="button-controls">
            <span class="list-controls">
                <a href="<?php
                    echo esc_url(add_query_arg(
                        array(
                            $post_type_name . \'-tab\' => \'all\',
                            \'selectall\' => 1,
                        ),
                        remove_query_arg($removed_args)
                    ));
                ?>#posttype-<?php echo $post_type_name; ?>" class="select-all"><?php _e(\'Select All\'); ?></a>
            </span>

            <span class="add-to-menu">
                <img class="waiting" src="<?php echo esc_url( admin_url( \'images/wpspin_light.gif\' ) ); ?>" alt="" />
                <input type="submit"<?php disabled( $nav_menu_selected_id, 0 ); ?> class="button-secondary submit-add-to-menu" value="<?php esc_attr_e(\'Add to Menu\'); ?>" name="add-post-type-menu-item" id="submit-posttype-<?php echo $post_type_name; ?>" />
            </span>
        </p>

    </div><!-- /.posttypediv -->
    <?php
}

结束

相关推荐

WP-ADMIN似乎正在重定向

我的w-admin登录有一个奇怪的问题。这是从我升级到3.0以后才开始的,当我转到wp admin时,登录表单显示正常,但当我输入用户名并通过时,每次都会再次显示登录表单。使用密码恢复功能会导致电子邮件未找到错误。我知道用户名密码和电子邮件是正确的,b/c我可以访问mysql数据库,我可以看到值(至少用户名和电子邮件) 有人知道会出什么问题吗