是否将页面上的单个菜单功能扩展为页面上的单个子菜单?

时间:2014-09-27 作者:Thomas Schneider

WordPress开发似乎正在建立一个关于最佳功能的知识库。非常感谢这已经帮了大忙了!

我找到了一个函数(在下面找到),它在一个框中为wordpress主题的每一页提供了一个简单的菜单决定。一个神奇的袋子戏法,向彼得·普费弗致敬!

但如何在子标题菜单-主题位置中更改子标题菜单,而不是页面上的所有菜单都是胡说八道。

当函数返回$args时,我想知道如何告诉Wp我只想让他们更改子标题菜单位置中的菜单。你有什么想法或其他功能,会很大!

/**
 * Erstellt eine Meta Box um einer Seite ein eigenes Menü mit zu geben.
 * Die Menüs müssen unter Design -> Menü definiert sein.
 *
 * @author H.-Peter Pfeufer
 */
if(!class_exists(\'Page_Menu_Meta_Box\')) {
    class Page_Menu_Meta_Box {
        private $textdomain = \'meine-textdomain\';   // Textdomain für die Übersetzung
        private $posttype = \'page\';                 // Posttype (hier page für Seiten)
        private $metaname = \'page_menu\';            // Name des Custom Fields
        private $metaboxID = \'page_menu\';           // ID der Metabox
        private $noncename = \'page_menu\';           // Name des Nonce (etwas für die Sicherheit
        private $defaultmenu = \'default\';           // "Slug" des Standardmenüs
        private $userright = \'edit_page\';           // Nutzerrechte die benötigt werden
        private $selectname = \'page-menu\';          // Name des Selectferldes

        /**
         * Constructor (old style)
         *
         * @uses __construct
         */
        function Page_Menu_Meta_Box() {
            self::__construct();
        } // END function Page_Menu_Meta_Box()

        /**
         * Constructor
         */
        function __construct() {
            // Backend
            if(is_admin()) {
                add_action(\'add_meta_boxes\', array(
                    &$this,
                    \'_add_meta_box\'
                ));

                add_action(\'save_post\', array(
                    &$this,
                    \'_save_page_menu\'
                ));
            } // END if(is_admin())

            // Frontend
            if(!is_admin()) {
                add_filter(\'wp_nav_menu_args\', array(
                    &$this,
                    \'_menu_per_page\'
                ));
            } // END if(!is_admin())
        } // END function __construct()

        /**
         * Metabox am System anmelden
         */
        function _add_meta_box() {
            add_meta_box($this->metaboxID, __(\'Select the menu for this page\', $this->textdomain), array(
                &$this,
                \'the_meta_box\'
            ), $this->posttype, \'normal\', \'high\');
        } // END function _add_meta_box()

        /**
         * Metabox erstellen
         */
        function the_meta_box() {
            global $post;

            // Menüs abholen
            $menues = wp_get_nav_menus();

            if(!empty($menues) && count($menues) != 0) {
                // Use nonce for verification
                wp_nonce_field(plugin_basename( __FILE__ ), $this->noncename);

                // Ist bereits ein Menü gewählt?
                $menuslug = get_post_meta($post->ID, $this->metaname, true);

                // Setting the defaultmenu
                if(empty($menuslug)) {
                    $menuslug = $this->defaultmenu;
                } // END if(empty($menu_name))

                echo sprintf(\'<p>%1$s</p>\', __(\'Please select the menu which should be displayed on this page.\', $this->textdomain));
                echo \'<select name="\' . $this->selectname . \'">\';

                foreach($menues as $menu) {
                    if(!empty($menuslug)) {
                        $selected = \'\';

                        if($menuslug == $menu->slug) {
                            $selected = \' selected="selected"\';
                        } // END if($menu_name == $menu->slug)
                    } // END if(!empty($menu_name))

                    echo \'<option value="\' . $menu->slug . \'"\' . $selected . \'>\' . $menu->name . \'</option>\';
                } // END foreach($menues as $menu)

                echo \'</select>\';
            } // END if(!empty($menues) && count($menues) != 0)
        } // END function the_meta_box()

        /**
         * Daten speichern
         *
         * @param int $post_id
         */
        function _save_page_menu($post_id) {
            // Erst mal schauen wir, ob der Nutzer das überhaupt darf
            if(!current_user_can($this->userright, $post_id)) {
                return;
            }

            // Dann prüfen wir die Nonces
            if(!isset($_REQUEST[$this->noncename]) || !wp_verify_nonce($_REQUEST[$this->noncename], plugin_basename(__FILE__))) {
                return;
            } // END if(!isset($_REQUEST[\'vokabel_page_menu\']) || !wp_verify_nonce($_REQUEST[\'vokabel_page_menu\'], plugin_basename(__FILE__)))

            // und nun wird der ganze Hokuspokus gespeichert
            $post_id = $_REQUEST[\'post_ID\'];

            // Metainformationen hinzufügen oder aktualisieren
            add_post_meta($post_id, $this->metaname, $_REQUEST[$this->selectname], true) or update_post_meta($post_id, $this->metaname, $_REQUEST[$this->selectname]);
        } // END function _save_page_menu($post_id)

        /**
         * Menü im Frontend anzeigen
         *
         * @param array $args
         * @return Ambigous <mixed, string, multitype:, boolean, array, string>
         */
        function _menu_per_page($args = \'\') {
            if(is_page()) {
                global $post;

                $menuslug = get_post_meta($post->ID, $this->metaname, true);

                if(!empty($menuslug) && is_nav_menu($menuslug)) {
                    $args[\'menu\'] = $menuslug;
                } // END if(!empty($menu_name) && is_nav_menu($menu_name))
            } // END if(is_page())

            return $args;
        } // END function _menu_per_page($args = \'\')
    } // END class Page_Menu_Meta_Box

    // Klasse starten
    new Page_Menu_Meta_Box();

} // END if(!class_exists(\'Page_Menu_Meta_Box\'))

2 个回复
SO网友:Nicolai Grossherr

如果这是关于指定$theme_location 对于wp_nav_menu(), 这样就足以检查它是否位于正确的位置。并且只修改$args_menu_per_page() 函数,该函数用作wp_nav_menu_args 滤器所以函数如下所示:

    function _menu_per_page( $args ) {
        if(is_page()) {
            // the selected menu applies to the following location
            if ( $args[\'theme_location\'] == \'sub-header-menu\' ) {
                global $post;

                $menuslug = get_post_meta($post->ID, $this->metaname, true);

                if(!empty($menuslug) && is_nav_menu($menuslug)) {
                    $args[\'menu\'] = $menuslug;
                } // END if(!empty($menu_name) && is_nav_menu($menu_name))
            } // END if ( $args[\'theme_location\'] == \'sub-header-menu\' )
        } // END if(is_page())

        return $args;
    } // END function _menu_per_page($args = \'\')

SO网友:Thomas Schneider

解决方案现在包含在插件中!

    <?php
/**
 * Plugin Name: CE WP-Menu per Page
 * Plugin URI: http://ppfeufer.de/wordpress-plugin/ce-wp-menu-per-page/
 * Description: This plugin allows you to select a menu on a per page basis.
 * Version: 1.2
 * Author: Codeenterprise (H.-Peter Pfeufer)
 * Author URI: http://codeenterprise.de
 * Text Domain: ce-wp-menu-per-page
 * Domain Path: /l10n
 */


        /**
         * Constructor (old style)
         *
         * @uses __construct
         */
        function Ce_Wp_Menu_Per_Page() {
            self::__construct();
        } // END function Ce_Wp_Menu_Per_Page()

        /**
         * Constructor
         */
        function __construct() {
            // Backend
            if(is_admin()) {
                add_action(\'admin_init\', array(
                    $this,
                    \'_plugin_init\'
                ));

                add_action(\'add_meta_boxes\', array(
                    $this,
                    \'_add_meta_box\'
                ));

                add_action(\'save_post\', array(
                    $this,
                    \'_save_page_menu\'
                ));
            } // END if(is_admin())

            // Frontend
            if(!is_admin()) {
                add_filter(\'wp_nav_menu_args\', array(
                    $this,
                    \'_menu_per_page\'
                ));
            } // END if(!is_admin())
        } // END function __construct()

        function _plugin_init() {
            /**
             * Sprachdatei wählen
             */
            if(function_exists(\'load_plugin_textdomain\')) {
                load_plugin_textdomain($this->textdomain, false, dirname(plugin_basename( __FILE__ )) . \'/l10n/\');
            } // END if(function_exists(\'load_plugin_textdomain\'))
        } // END function _plugin_init()

        /**
         * Metabox am System anmelden
         */
        function _add_meta_box() {
            add_meta_box($this->metaboxID, __(\'Select the menu for this page\', $this->textdomain), array(
                $this,
                \'the_meta_box\'
            ), $this->posttype, \'normal\', \'high\');
        } // END function _add_meta_box()

        /**
         * Metabox erstellen
         */
        function the_meta_box() {
            global $post;

            // Menüs abholen
            $menues = wp_get_nav_menus();
            $menu_locations = get_registered_nav_menus();

            if(!empty($menues) && count($menues) != 0) {
                // Use nonce for verification
                wp_nonce_field(plugin_basename( __FILE__ ), $this->noncename);

                // Ist bereits ein Menü gewählt?
                $menuslug = get_post_meta($post->ID, $this->metaname_menu, true);
                $menuposition = get_post_meta($post->ID, $this->metaname_menu_position, true);

                // Setting the defaultmenu
                if(empty($menuslug)) {
                    $menuslug = $this->defaultmenu;
                } // END if(empty($menu_name))

                $array_DefaultMenu[\'wp-default\'] = new stdClass();
                $array_DefaultMenu[\'wp-default\']->name = __(\'Wordpress Default\', $this->textdomain);
                $array_DefaultMenu[\'wp-default\']->slug = \'wp-default\';

                $menues = array_merge($array_DefaultMenu, (array) $menues);

                echo sprintf(\'<p>%1$s</p>\', __(\'Please select the menu which should be displayed on this page.\', $this->textdomain));
                echo \'<select name="\' . $this->menu_selectname . \'">\';

                foreach($menues as $menu) {
                    if(!empty($menuslug)) {
                        $selected = \'\';

                        if($menuslug == $menu->slug) {
                            $selected = \' selected="selected"\';
                        } // END if($menu_name == $menu->slug)
                    } // END if(!empty($menu_name))

                    echo \'<option value="\' . $menu->slug . \'"\' . $selected . \'>\' . $menu->name . \'</option>\';
                } // END foreach($menues as $menu)

                echo \'</select>\';

                if(is_array($menu_locations) && count($menu_locations) != 0) {
                    echo sprintf(\'<p>%1$s</p>\', __(\'Please select the menu location, where your menu should appear\', $this->textdomain));
                    echo \'<select name="\' . $this->position_selectname . \'">\';

                    foreach($menu_locations as $slug => $position) {
                        if(!empty($menuposition)) {
                            $selected = \'\';

                            if($menuposition == $slug) {
                                $selected = \' selected="selected"\';
                            } // END if($menu_name == $menu->slug)
                        } // END if(!empty($menu_name))

                        echo \'<option value="\' . $slug . \'"\' . $selected . \'>\' . $position . \'</option>\';
                    } // END foreach($menues as $menu)

                    echo \'</select>\';
                } // END if(is_array($menu_locations) && count($menu_locations) != 0)
            } // END if(!empty($menues) && count($menues) != 0)
        } // END function the_meta_box()

        /**
         * Daten speichern
         *
         * @param int $post_id
         */
        function _save_page_menu($post_id) {
            // Erst mal schauen wir, ob der Nutzer das überhaupt darf
            if(!current_user_can($this->userright, $post_id)) {
                return;
            }

            // Dann prüfen wir die Nonces
            if(!isset($_REQUEST[$this->noncename]) || !wp_verify_nonce($_REQUEST[$this->noncename], plugin_basename(__FILE__))) {
                return;
            } // END if(!isset($_REQUEST[\'vokabel_page_menu\']) || !wp_verify_nonce($_REQUEST[\'vokabel_page_menu\'], plugin_basename(__FILE__)))

            // und nun wird der ganze Hokuspokus gespeichert
            $post_id = $_REQUEST[\'post_ID\'];

            /**
             * Saving the Settings
             */
            if($_REQUEST[$this->menu_selectname] == $this->defaultmenu) {
                // Get the old slug
                $menuslug = get_post_meta($post->ID, $this->metaname_menu, true);

                if(empty($menuslug)) {
                    delete_post_meta($post_id, $this->metaname_menu);
                    delete_post_meta($post_id, $this->metaname_menu_position);
                } // END if(empty($menu_name))
            } else {
                // Metainformationen hinzufügen oder aktualisieren
                add_post_meta($post_id, $this->metaname_menu, $_REQUEST[$this->menu_selectname], true) or update_post_meta($post_id, $this->metaname_menu, $_REQUEST[$this->menu_selectname]);
                add_post_meta($post_id, $this->metaname_menu_position, $_REQUEST[$this->position_selectname], true) or update_post_meta($post_id, $this->metaname_menu_position, $_REQUEST[$this->position_selectname]);
            } // END // END if($_REQUEST[$this->menu_selectname] == $this->defaultmenu)
        } // END function _save_page_menu($post_id)

        /**
         * Menü im Frontend anzeigen
         *
         * @param array $args
         * @return Ambigous <mixed, string, multitype:, boolean, array, string>
         */
        function _menu_per_page($args = \'\') {
            global $post;

            if(!empty($post)) {
                $menuslug = get_post_meta($post->ID, $this->metaname_menu, true);
                $menuposition = get_post_meta($post->ID, $this->metaname_menu_position, true);

                if(empty($menuposition)) {
                    $menuposition = $this->defaultmenu_position;
                } // END if(empty($menuposition))

                if(is_page() && $args[\'theme_location\'] == $menuposition) {
                    if(!empty($menuslug) && is_nav_menu($menuslug)) {
                        $args[\'menu\'] = $menuslug;
                    } // END if(!empty($menu_name) && is_nav_menu($menu_name))
                } // END if(is_page())
            } // END if(!empty($post))

            return $args;
        } // END function _menu_per_page($args = \'\')
    } // END class Ce_Wp_Menu_Per_Page

    // Klasse starten
    new Ce_Wp_Menu_Per_Page();
} // END if(!class_exists(\'Ce_Wp_Menu_Per_Page\'))

结束

相关推荐

How to add taxonomy in menus?

书籍(自定义帖子类型)小说(税)科学(税)历史(税)--书籍体裁(税务)小说(术语)科学(学期)历史(学期)以下哪一项是做这件事的“好方法”?对于前一个(这是我目前在管理菜单中的功能,我为每个功能都提供了“register\\u taxonomy”功能),我无法选择要在菜单中显示的“Tax”。而对于后者,我可以将它们添加到菜单中,只需要一个“register\\u taxonomy”函数。