有没有一种简单的方法可以用页面或帖子替换自定义菜单链接?

时间:2015-11-06 作者:Aziz

我一直在寻找插件,但找不到任何东西,我相信这应该是一个核心功能。

The Problem:

当前替换自定义链接或任何菜单链接的方法是执行以下过程:

删除旧的菜单链接插入新链接从列表末尾拖动新链接将新链接拖放到所需位置重复步骤3和4,直到中大奖为止

Why is it a problem

这是非常低效的,尤其是当:(a)处理巨大的菜单(b)具有许多子级的菜单(c)替换许多具有自定义选项的菜单项时

Solution requirements

  1. 保留菜单位置/层次结构
  2. 保留选项(css类、标签、标题)
  3. 从页面/帖子/类别等中选择

    Demonstration

    http://puu.sh/laSEi/81b0d41705.png

    Should be that simple:

    enter image description here

    其他可能的想法是复制/“将子链接添加到此”/或者甚至将新链接添加到列表的顶部而不是底部。

    感谢您的反馈。

3 个回复
SO网友:cbos

虽然这并不能直接回答问题,但它所基于的代码提供了功能。代码集为:

function install_menus() {
    require_once dirname( __FILE__) . \'/data.php\';
    $menus = get_menus_data();
    if ( ! empty ( $menus ) ) foreach ( $menus as $menu ) {
        if ( $menu[\'build\'] ) {
            $menu_id = create_nav_menu( $menu );
            add_items_to_menu( $menu_id, $menu[\'slug\'], $menu[\'items\'] );
        }
    }
}

function create_nav_menu( $menu ) {
    if ( $exists = wp_get_nav_menu_object( $menu[\'name\'] ) ) {
        $menu_id = $exists -> term_id;
          if ( empty ( $menu_id ) ) {
            $menu_id = wp_create_nav_menu( $menu[\'name\'] );
        } 
    }
    else {
        $menu_id = wp_create_nav_menu( $menu[\'name\'] );
    }
    return $menu_id;
}
function add_items_to_menu( $menu_id, $slug, $items ) {
    if ( $items ) foreach ( $items as $item ) {
        if ( $item[\'build\'] ) {
            $slug = ( $item[\'title\'] == \'Home\' ) ? \'home\' : $item[\'slug\'];
            if ( ! menu_item_exists( $slug, $menu_id ) ) {
                wp_update_nav_menu_item( $menu_id, 0, array (
                    \'menu-item-title\' =>  __( $item[\'title\'] ),
                    \'menu-item-classes\' => \'\',
                    \'menu-item-url\' => home_url( $item[\'slug\'] . \'/\' ), 
                    \'menu-item-status\' => \'publish\'
                    ) );
            }
        }
    }
}
function menu_item_exists( $slug, $menu_id ) {
    $args = array(
        \'order\'                  => \'ASC\',
        \'orderby\'                => \'menu_order\',
        \'post_type\'              => \'nav_menu_item\',
        \'post_status\'            => \'publish\',
        \'output\'                 => ARRAY_A,
        \'output_key\'             => \'menu_order\',
        \'nopaging\'               => true,
        \'update_post_term_cache\' => false ); 

    $existing = wp_get_nav_menu_items( $menu_id, $args );
    $found = false;
    foreach ( $existing as $exists ) {
        if( strpos( $exists->post_name, $slug ) !== FALSE  ) {  //pretty good search (not exact).
            $found = true;
            break;
        }

    }
    return $found;
}
数据文件为:

function get_menus_data() {
    $items = array ( 
        array ( 
            \'name\' => \'Main Menu\', \'slug\' => \'main-menu\', \'build\' => 1, 
            \'items\' => array (
                array ( \'title\' => \'Home\', \'slug\' => \'\', \'build\' => 1 ), //slug should be empty
                array ( \'title\' => \'Blog\', \'slug\' => \'blog\', \'build\' => 1 ),
                array ( \'title\' => \'About\', \'slug\' => \'about\', \'build\' => 1 ),
                array ( \'title\' => \'Contact\', \'slug\' => \'contact\', \'build\' => 1 ),
                ),
        ),
        array ( 
            \'name\' => \'Secondary Menu\', \'slug\' => \'secondary-menu\', \'build\' => 0,
            \'items\' => array (
                array ( \'title\' => \'Home\', \'slug\' => \'\', \'build\' => 1 ),
                array ( \'title\' => \'Blog\', \'slug\' => \'blog\', \'build\' => 1 ),
                array ( \'title\' => \'About\', \'slug\' => \'about\', \'build\' => 1 ),
                array ( \'title\' => \'Contact\', \'slug\' => \'contact\', \'build\' => 1 ),
                ),
        ),
        array ( 
            \'name\' => \'Footer Menu\', \'slug\' => \'footer-menu\', \'build\' => 1,
            \'items\' => array (
                array ( \'title\' => \'Terms\', \'slug\' => \'terms\', \'build\' => 1 ),
                array ( \'title\' => \'Privacy\', \'slug\' => \'privacy\', \'build\' => 1 ),
                array ( \'title\' => \'Contact\', \'slug\' => \'contact\', \'build\' => 1 ),
                ),
            ) 
    );
    return $items;
}
需要在此基础上构建一个接口,以允许所请求的选择,但此代码正在运行并经过测试。

SO网友:Tim Plummer

我不确定这是一个答案,但更多的是一个讨论点。

有没有人考虑过构建WP菜单的高级自定义字段?我已经做过几次了,它允许我构建一个自定义结构以及自定义菜单项属性,并构建菜单的HTML,而无需使用默认WP菜单所需的复杂walker。

enter image description here

ACF

if( function_exists(\'acf_add_local_field_group\') ):

acf_add_local_field_group(array (
    \'key\' => \'group_56532ec144a4b\',
    \'title\' => \'Menu\',
    \'fields\' => array (
        array (
            \'key\' => \'field_5653338918f43\',
            \'label\' => \'Menus\',
            \'name\' => \'menus\',
            \'type\' => \'flexible_content\',
            \'instructions\' => \'\',
            \'required\' => 0,
            \'conditional_logic\' => 0,
            \'wrapper\' => array (
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'button_label\' => \'Add Menu\',
            \'min\' => \'\',
            \'max\' => \'\',
            \'layouts\' => array (
                array (
                    \'key\' => \'56533396b10bc\',
                    \'name\' => \'menu\',
                    \'label\' => \'Menu\',
                    \'display\' => \'block\',
                    \'sub_fields\' => array (
                        array (
                            \'key\' => \'field_56533fc6f25e7\',
                            \'label\' => \'Menu Name\',
                            \'name\' => \'menu__name\',
                            \'type\' => \'text\',
                            \'instructions\' => \'\',
                            \'required\' => 0,
                            \'conditional_logic\' => 0,
                            \'wrapper\' => array (
                                \'width\' => \'\',
                                \'class\' => \'\',
                                \'id\' => \'\',
                            ),
                            \'default_value\' => \'\',
                            \'placeholder\' => \'\',
                            \'prepend\' => \'\',
                            \'append\' => \'\',
                            \'maxlength\' => \'\',
                            \'readonly\' => 0,
                            \'disabled\' => 0,
                        ),
                        array (
                            \'key\' => \'field_56532ec718f40\',
                            \'label\' => \'Menu Items\',
                            \'name\' => \'menu__items\',
                            \'type\' => \'flexible_content\',
                            \'instructions\' => \'\',
                            \'required\' => 0,
                            \'conditional_logic\' => 0,
                            \'wrapper\' => array (
                                \'width\' => \'\',
                                \'class\' => \'\',
                                \'id\' => \'\',
                            ),
                            \'button_label\' => \'Add Menu Item\',
                            \'min\' => \'\',
                            \'max\' => \'\',
                            \'layouts\' => array (
                                array (
                                    \'key\' => \'56532eee6ef81\',
                                    \'name\' => \'menuItem\',
                                    \'label\' => \'Menu Item\',
                                    \'display\' => \'block\',
                                    \'sub_fields\' => array (
                                        array (
                                            \'key\' => \'field_56532f0418f41\',
                                            \'label\' => \'Label\',
                                            \'name\' => \'menuITem__label\',
                                            \'type\' => \'text\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => 0,
                                            \'wrapper\' => array (
                                                \'width\' => 50,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'default_value\' => \'\',
                                            \'placeholder\' => \'\',
                                            \'prepend\' => \'\',
                                            \'append\' => \'\',
                                            \'maxlength\' => \'\',
                                            \'readonly\' => 0,
                                            \'disabled\' => 0,
                                        ),
                                        array (
                                            \'key\' => \'field_565333d218f45\',
                                            \'label\' => \'Class\',
                                            \'name\' => \'menuItem__class\',
                                            \'type\' => \'text\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => 0,
                                            \'wrapper\' => array (
                                                \'width\' => 50,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'default_value\' => \'\',
                                            \'placeholder\' => \'\',
                                            \'prepend\' => \'\',
                                            \'append\' => \'\',
                                            \'maxlength\' => \'\',
                                            \'readonly\' => 0,
                                            \'disabled\' => 0,
                                        ),
                                        array (
                                            \'key\' => \'field_565342ef11b29\',
                                            \'label\' => \'Link Type\',
                                            \'name\' => \'menuItem__type\',
                                            \'type\' => \'radio\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => 0,
                                            \'wrapper\' => array (
                                                \'width\' => 25,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'choices\' => array (
                                                \'page\' => \'Page\',
                                                \'cat\' => \'Category\',
                                                \'url\' => \'URL\',
                                                \'cust\' => \'Custom\',
                                            ),
                                            \'other_choice\' => 0,
                                            \'save_other_choice\' => 0,
                                            \'default_value\' => \'\',
                                            \'layout\' => \'vertical\',
                                        ),
                                        array (
                                            \'key\' => \'field_56532f2d18f42\',
                                            \'label\' => \'Page\',
                                            \'name\' => \'menuItem__page\',
                                            \'type\' => \'page_link\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => array (
                                                array (
                                                    array (
                                                        \'field\' => \'field_565342ef11b29\',
                                                        \'operator\' => \'==\',
                                                        \'value\' => \'page\',
                                                    ),
                                                ),
                                            ),
                                            \'wrapper\' => array (
                                                \'width\' => 75,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'post_type\' => array (
                                            ),
                                            \'taxonomy\' => array (
                                            ),
                                            \'allow_null\' => 0,
                                            \'multiple\' => 0,
                                        ),
                                        array (
                                            \'key\' => \'field_5653434f11b2a\',
                                            \'label\' => \'Category\',
                                            \'name\' => \'menuItem__cat\',
                                            \'type\' => \'taxonomy\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => array (
                                                array (
                                                    array (
                                                        \'field\' => \'field_565342ef11b29\',
                                                        \'operator\' => \'==\',
                                                        \'value\' => \'cat\',
                                                    ),
                                                ),
                                            ),
                                            \'wrapper\' => array (
                                                \'width\' => 75,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'taxonomy\' => \'category\',
                                            \'field_type\' => \'select\',
                                            \'allow_null\' => 0,
                                            \'add_term\' => 1,
                                            \'save_terms\' => 0,
                                            \'load_terms\' => 0,
                                            \'return_format\' => \'id\',
                                            \'multiple\' => 0,
                                        ),
                                        array (
                                            \'key\' => \'field_5653439311b2c\',
                                            \'label\' => \'Custom\',
                                            \'name\' => \'menuItem__cstm\',
                                            \'type\' => \'text\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => array (
                                                array (
                                                    array (
                                                        \'field\' => \'field_565342ef11b29\',
                                                        \'operator\' => \'==\',
                                                        \'value\' => \'cust\',
                                                    ),
                                                ),
                                            ),
                                            \'wrapper\' => array (
                                                \'width\' => 75,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'default_value\' => \'\',
                                            \'placeholder\' => \'\',
                                            \'prepend\' => \'\',
                                            \'append\' => \'\',
                                            \'maxlength\' => \'\',
                                            \'readonly\' => 0,
                                            \'disabled\' => 0,
                                        ),
                                        array (
                                            \'key\' => \'field_5653437011b2b\',
                                            \'label\' => \'URL\',
                                            \'name\' => \'menuItem__url\',
                                            \'type\' => \'url\',
                                            \'instructions\' => \'\',
                                            \'required\' => 0,
                                            \'conditional_logic\' => array (
                                                array (
                                                    array (
                                                        \'field\' => \'field_565342ef11b29\',
                                                        \'operator\' => \'==\',
                                                        \'value\' => \'url\',
                                                    ),
                                                ),
                                            ),
                                            \'wrapper\' => array (
                                                \'width\' => 75,
                                                \'class\' => \'\',
                                                \'id\' => \'\',
                                            ),
                                            \'default_value\' => \'\',
                                            \'placeholder\' => \'\',
                                        ),
                                    ),
                                    \'min\' => \'\',
                                    \'max\' => \'\',
                                ),
                            ),
                        ),
                    ),
                    \'min\' => \'\',
                    \'max\' => \'\',
                ),
            ),
        ),
    ),
    \'location\' => array (
        array (
            array (
                \'param\' => \'options_page\',
                \'operator\' => \'==\',
                \'value\' => \'acf-options-theme-options\',
            ),
        ),
    ),
    \'menu_order\' => 0,
    \'position\' => \'normal\',
    \'style\' => \'default\',
    \'label_placement\' => \'top\',
    \'instruction_placement\' => \'label\',
    \'hide_on_screen\' => \'\',
    \'active\' => 1,
    \'description\' => \'\',
));

endif;

UX

function acfMenu($name) {
    if( function_exists(\'get_field\') ) :
        $getMenus = get_field(\'menus\', \'option\');
        foreach($getMenus as $menuData) : 
            if( $menuData[\'menu__name\'] == $name ) : 
              // Do stuff to build your menu
            endif;
        endforeach;
    endif;
}
这只是一个简单的示例,但使用ACF提供的选项,您可以将各种内容添加到菜单项上,然后随意编写UI。

在应用于您的特定问题时,可以依次将条件选择应用于您提供的链接类型(请参见附图)。ACF允许许多不同的参数,如页面链接、类别链接或直接URL。有条件地选择这些项将允许更改菜单项的类型,而无需删除类或其他属性。

SO网友:garth

这可能有助于确定正确的方法。

WP背后的虚设哲学首先推动了他们拥有设置菜单的方式,这也是为什么WP作为处理内容经常变化的大量网站的框架是一个糟糕的选择的原因之一。

在试图使内容管理尽可能成为虚拟证据的过程中,它将其锁定在特定的范式中,而这些范式通常会产生额外的工作,而且往往没有充分的理由。在没有配置丢失的情况下,您无法保存菜单模板,也无法复制现有菜单,也无法安全存储菜单中可重复使用的菜单项。

我喜欢你的建议,这将是他们提供的本机菜单格式的一个很好的替代方案,因为它是防伪方法之间的一个很好的中间地带,同时仍然提供了更多的灵活性和部署速度。我会添加一个排序参数,作为烦人的拖放过程的替代方法,这个过程可能会造成很多麻烦。

然而,虽然我希望WP中的菜单管理有所改变,但在这条道路上走得太远违背了WP的核心理念,可能意味着它不再迎合最低的共同标准,而这正是WP流行的主要原因。

相关推荐

Add extra markup to WP menus

我被要求将一个HTML网站重新开发为一个WP主题,它有一个非常高级的菜单结构,我不太确定如何复制它。第二个菜单项有一个子菜单,但它不仅仅是“li”中的“ul”,还有额外的div等。。因为下拉列表有3列布局,其中2列包含子链接,第3列包含内容。这是一个示例:<ul class=\"nav navbar-nav three\"> <li class=\"dropdown yamm-fw\"> <a href=\"#\" class=\"dropdown-toggl