WordPressÖÐÖÆ×÷µ¼º½²Ëµ¥µÄPHPºËÐÄ·½·¨½²½â

5年以前  |  阅读数:799 次  |  编程语言:PHP 

WordPress 3.0 ÒýÈeµ¼º½²Ëµ¥¹¦ÄÜ, ÈÃÒ³ÃaeµÄµ¼º½ºÍÁ´½ÓµÄ¹ÜÀi±aµÃ¼oµ¥Ò×ÓÃ. WP ÏoÓû§Ìa¹(C)Á˲˵¥¹ÜÀiÒ³ÃaeºÍ¶aÖÖµ÷Óý¨, ÎÒÃÇÏÈÀ´¿´Ò»ÏÂÒ»°aWordPress µÄµ¼º½²Ëµ¥¶¼¾ßÓÐÄÄÐ(C)¹¦ÄÜ.

¹ÜÀi²Ëµ¥Ò³Ãae

Ò³Ãae¾¶: Appearance > Menus
ÓÉ´Ó½çÃaeÀ´¿´, Äܹ»´´½¨¶a¸o²Ëµ¥, ²¢ÇÒ¿ÉÒÔÏo²Ëµ¥ÖÐÌi¼Ó×Ô¶¨ÒaÁ´½Ó, Ò³ÃaeÁ´½ÓºÍ
ÖÀaÁ´½Ó.

20151211142006684.png \(600¡Á375\)

µ«ÓÐÒ»µaÐeҪעÒa, Èç¹u½«Ò³ÃaeÁ´½ÓºÍÖÀaÁ´½Ó¼ÓÈe, ±»´øÈe²Ëµ¥µÄ½oÊÇÁ´½Ó, ¶ø²»ÊÇÒ³ÃaeºÍÖÀa±¾Éi, Ò²¾ÍÊÇ˵, ×ÓÒ³ÃaeºÍ×ÓÖÀa²»»a³ÉΪ²Ëµ¥µÄÒ»²¿Ö.

ÁiÍa, ÎÒÃÇÒ²²»ÄÜÔڲ˵¥ÖмÓÈe±ðµÄ²Ëµ¥, ËuÒÔÕaÖÖ×Ô¶¨ÒaµÄ²Ëµ¥×¢¶¨Ö»´aeÔÚÒ»¼¶. ÔÚÓÒ±ßÅÅÁв˵¥Çø¿eÖÐ, Ö»Òª½«Ä³¸o²Ëµ¥ÍuÓÒÒÆÒ»¸ñ, ¾Í¿ÉÒÔ×÷Ϊϼ¶²Ëµ¥. ËuÒÔÊÇ¿ÉÒÔ´´½¨¶a¼¶µÄ.
´´½¨²Ëµ¥ºo, ÎÒÃǾͿÉÒÔͨ¹ýÔÚ Widgets Ò³Ãae½«ÕaÐ(C)²Ëµ¥Ìi¼Óµ½²a±ßÀ¸ÉÏÁË.

20151211142032145.png \(270¡Á90\)

µÇ¼Ç×Ô¶¨Òa²Ëµ¥

Ôڲ˵¥¹ÜÀiÒ³ÃaeÖÐ×¢Òaµ½ Theme Locations Çø¿eÌaʾÈçÏÂ:


    The current theme does not natively support menus, but you can use the "Custom Menu" widget to add any menus you create here to the theme's sidebar.

Òa˼ÊÇ˵, ÏÖÔÚÄaµÄÖ÷Ìa²»Ö§³Ö×Ô¶¨Òa²Ëµ¥, µ«¿ÉÒÔͨ¹ý Widget µÄ*½Ê½½«×Ô¶¨Òa¼Óµ½Ö÷ÌaµÄ²a±ßÀ¸. ÄÇÈçºÎÈÃÖ÷ÌaÒ²ÄÜÖ§³Ö×Ô¶¨Òa²Ëµ¥ÄØ? Çe½«ÒÔÏ´uÂe¼Óµ½ function.php ÖÐ.


    register_nav_menus(array(
     'primary' => 'Primary Navigation'
    ));

Õa¶Î´uÂeÓÃÓڼǼһ¸o×Ô¶¨Òa²Ëµ¥, Äa¿ÉÒÔΪËuÑ¡Ôñ¾ßÌaµÄÓ¦Óò˵¥, ÆaÖÐ primary Ó¦ÊÇΨһʶ±ðu, Primary Navigation ÊDz˵¥µÄÃu³Æ. ¿ÉÒÔͨ¹ýÕa¸oº¯ÊýΪÖ÷ÌaÌi¼Ó¶a¸o×Ô¶¨Òa²Ëµ¥. Èç¹uÔÚ function.php Ìi¼ÓµÇ¼Ç½*¨ÈçÏÂ:


    register_nav_menus(array('primary' => 'Primary Navigation'));
    register_nav_menus(array('secondary' => 'Secondary Navigation'));
    register_nav_menus(array('bottom' => 'Bottom Navigation'));

Ôڲ˵¥¹ÜÀiÒ³Ãae½«»a¿´µ½ÈçÏÂͼµÄÄÚÈÝ.

20151211142105140.png \(292¡Á301\)

Ö÷Ìaµ÷Óò˵¥

ÔÚÖ÷ÌaÖÐÈçºÎµ÷Óò˵¥ÄØ? ÔÚÖ÷ÌaµÄÊʺÏλÖÃʹÓý¨ wp_nav_menu();, ÏoÒ³ÃaeÊa³o²Ëµ¥.
½¨ÖÐÌa¹(C)²ÎÊý theme_location, ÓÃÓÚÖ¸¶¨¶ÔÓ¦µÄ×Ô¶¨Òa²Ëµ¥. ÈçÒªµ÷ÓõÚÒ»¸o²Ëµ¥, Ôo´uÂeÈçÏÂ:


    wp_nav_menu(array(
     'theme_location' =>'primary'
    ));

ÔÚĬÈÏÇe¿oÏÂÈç¹uûÓж¨ÒaÈκβ˵¥, ʹÓà wp_nav_menu ½¨Óe wp_list_pages (µ÷ÓÃÒ³ÃaeÁбi) ½¨ÎÞÒi, µ«Ð§Âʲ»¼°ºoÕß. ËuÒÔ, Èç¹uÄa×¼±¸ÓÃÒ³ÃaeÁбi»oÕß*ÖÀaÁбi×÷Ϊµ¼º½²Ëµ¥, ½¨Òe²»ÒªÊ¹Óà wp_nav_menu.

*Óè**

ÔÚÖ÷Ìaµ÷Óõ¼º½²Ëµ¥, ½¨ºÜ¼oµ¥. Ö»Ðe¼ÓÈeÒÔÏÂÓi¾aÔÚÒ³ÃaeÉÏÊa³o²Ëµ¥.


    <?php wp_nav_menu(); ?>

µ«ÆaʵÕa¸o½¨Ìa¹(C)Á˺ܶa¿ÉÅaÖõIJÎÊý, ÏÂÃaeÎÒÃÇÖðÒ»ÃeÊo.

²ÎÊý

²ÎÊýÁбiÀ´×Ô WordPress Codex, ÏÂÃaeÖðÒ»*­Òe, ²¢¶Ô²»Ò×Ài½aµÄ²ÎÊý½øÐÐÏeϸ˵Ã÷.

$menu
(×Ö*u´®)(¿ÉÑ¡) ÆÚÍuÏÔʾµÄ²Ëµ¥; ½ÓÊÜ (°´Ë³ÐoÆ¥ÅaµÄ) id, slug, name
ĬÈÏÖµ: None

ÎÒÃÇ¿´Ò»Ï WordPress È¡²Ëµ¥µÄ½¨. ¾ÍÏñ Codex ÉϵÄÃeÊoÒ»Ñu, ËuÊÇ°´ id, slug, name µÄ˳ÐoȥȡµÄ.


    function wp_get_nav_menu_object( $menu ) {
     // ûÓÐÌa¹(C)²ÎÊý, *µ»Ø¿Õ
     if ( ! $menu )
     return false;

     // ¸u¾Ý id ÕÒ
     $menu_obj = get_term( $menu, 'nav_menu' );

     // Èç¹uÕÒ²»µ½, ¸u¾Ý slug À´ÕÒ
     if ( ! $menu_obj )
     $menu_obj = get_term_by( 'slug', $menu, 'nav_menu' );

     // Èç¹u»¹ÕÒ²»µ½, ÔÙ¸u¾Ý name À´ÕÒ
     if ( ! $menu_obj )
     $menu_obj = get_term_by( 'name', $menu, 'nav_menu' );

     // ×iÖÕûÕÒµ½, *µ»Ø¿Õ
     if ( ! $menu_obj )
     $menu_obj = false;

     return $menu_obj;
    }

$container
(×Ö*u´®)(¿ÉÑ¡) ul ¸¸½ÚµaµÄ±eÇ(C)ÀaÐÍ
ĬÈÏÖµ: div

ǧÍo²»ÒªÒÔΪʲô±eÇ(C)¶¼¿ÉÒÔʹÓÃ, ÊÂʵÉÏÖ»ÓÐ div ºÍ nav »a±»²ÉÓÃ, Èç¹uÊaÈe±ðµÄÖµ, ul ¸¸½ÚµaµÄ±eÇ(C)½«²»»aÏÔʾ, ¿É¼u Codex µÄÃeÊo²»¹»Ïe¾¡. (´ÓÁiÒ»¸o½Ç¶È¿´, WordPress ʹÓà nav ±eÇ(C)˵Ã÷ËuÕýÔÚÌaÉý¶Ô HTML5 µÄÖ§³ÖÁ¦¶È.)


    // ±»ÔÊÐiʹÓõıeÇ(C)Ö»ÓÐ div ºÍ nav
    $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );

$container_class
(×Ö*u´®)(¿ÉÑ¡) ul ¸¸½ÚµaµÄ class ÊoÐÔÖµ
ĬÈÏÖµ: menu-{menu slug}-container

$container_id
(×Ö*u´®)(¿ÉÑ¡) ul ¸¸½ÚµaµÄ id ÊoÐÔÖµ
ĬÈÏÖµ: None

$menu_class
(×Ö*u´®)(¿ÉÑ¡) ul ½ÚµaµÄ class ÊoÐÔÖµ
ĬÈÏÖµ: menu

$menu_id
(×Ö*u´®)(¿ÉÑ¡) ul ½ÚµaµÄ id ÊoÐÔÖµ
ĬÈÏÖµ: menu slug, ×ÔÔo³¤µÄ

$echo
(²¼¶uÐÍ)(¿ÉÑ¡) ¾o¶¨Ö±½ÓÏÔʾ²Ëµ¥»¹ÊÇ*µ»Ø HTML Ƭ¶Î
ĬÈÏÖµ: true (Ö±½ÓÏÔʾ)

$fallback_cb
(×Ö*u´®)(¿ÉÑ¡) Èç¹u²Ëµ¥²»´aeÔÚ, µ÷ÓõĻص÷º¯Êý
ĬÈÏÖµ: wp_page_menu (ÏÔʾҳÃaeÁбi×÷Ϊ²Ëµ¥)

ÕaÊÇÒ»¸oºÜÖØÒªµÄ½¨, ¿ÉÒÔͨ¹ýËuÈ¥¼aeÈÝÀÏ°ae±¾Ö÷Ìa. ÏÂÃaeÎÒÃÇ¿´¿´´uÂe. ¹Ø¼uÊÇ $args Ò²±»´«Èe call_user_func ÖÐ. ÀýÈç, ÎÒÃǽ«²ÎÊý 'sort_column'=>'menu_order' дÈe wp_nav_menu µÄ²ÎÊý, ÄÇËuͬÑu»a±»´«µ½ call_user_func ½¨ÖÐ. Èç¹u call_user_func ÊÇ wp_page_menu ½¨, ÄÇôÏÔʾµÄÒ³ÃaeÁбi½«ÒÔÈÏΪ¸³ÓeµÄÐoºÅÀ´ÅÅÐoÊa³o.


    // Èç¹uÕÒ²»µ½Ö¸¶¨²Ëµ¥, »oÕ߲˵¥²»´aeÔÚÈκÎÌoÄ¿²¢Ã»ÓÐÖ¸¶¨×Ô¶¨Òa²Ëµ¥, ʹÓà call_user_func *½*¨À´½øÐд¦Ài
    if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
     && ( function_exists($args->fallback_cb) || is_callable( $args->fallback_cb ) ) )
     return call_user_func( $args->fallback_cb, (array) $args );

$before
(×Ö*u´®)(¿ÉÑ¡) ÏÔʾÔÚÿ¸o²Ëµ¥Á´½ÓÇ°µÄÎı¾
ĬÈÏÖµ: None

$after
(×Ö*u´®)(¿ÉÑ¡) ÏÔʾÔÚÿ¸o²Ëµ¥Á´½ÓºoµÄÎı¾
ĬÈÏÖµ: None

$link_before
(×Ö*u´®)(¿ÉÑ¡) ÏÔʾÔÚÿ¸o²Ëµ¥Á´½ÓÎı¾Ç°µÄÎı¾
ĬÈÏÖµ: None

$link_after
(×Ö*u´®)(¿ÉÑ¡) ÏÔʾÔÚÿ¸o²Ëµ¥Á´½ÓÎı¾ºoµÄÎı¾
ĬÈÏÖµ: None

ÎÒ»³ÒÉ Codex ¶Ô $before Óe $link_before, $after Óe $link_after µÄÃeÊoÊDz»Êǵ¹¹ýÀ´ÁË?

$depth
(ÕuÐÍ)(¿ÉÑ¡) ÏÔʾ²Ëµ¥µÄÉi¶È, µ±ÊýֵΪ 0 ʱÏÔʾËuÓÐ
ĬÈÏÖµ: 0

$walker
(×Ö*u´®)(¿ÉÑ¡) ×Ô¶¨ÒaµÄ±eÀu¶ÔÏo
ĬÈÏÖµ: None

$theme_locaton
(×Ö*u´®)(¿ÉÑ¡) the location in the theme to be used--must be registered with register_nav_menu() in order to be selectable by the user
ĬÈÏÖµ: None

Èç¹uÖ÷ÌaÔÚ function.php ÖеǼÇÁË 3 ¸o×Ô¶¨Òa²Ëµ¥, ÈçÏÂ:


    register_nav_menus(array('primary' => 'Primary Navigation'));
    register_nav_menus(array('secondary' => 'Secondary Navigation'));
    register_nav_menus(array('bottom' => 'Bottom Navigation'));

Òªµ÷Óà Secondary Navigation Õa¸oµ¼º½²Ëµ¥, Ôo¿ÉÒÔÔÚ header.php ÎļþÀiʹÓÃÒÔÏÂÓi¾a:


    wp_nav_menu(array(
     'theme_location' =>'secondary'
    ));

Ò²¾ÍÊÇ˵, ÕaÊÇÓÃÀ´Ö¸¶¨µ÷ÓÃij¸o×Ô¶¨Òa²Ëµ¥µÄ.

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例