公告:123目录网为广大站长提供免费收录网站服务,我们将免费进行到底,如需快审服务(10元/站)请联系站长QQ,可自助充值发布。

点击这里在线咨询客服
新站提交
  • 网站:315
  • 待审:0
  • 小程序:12
  • 文章:35791
  • 会员:69

WordPress 自定义菜单功能是非常好用的一个功能,但是我们在实际使用的过程当中,却发现一个非常严重的问题,效率非常低下,如果自定义菜单比较复杂的话,会产生多达十几条甚至几十条的 SQL 查询,我认真查看了下 wp_nav_menu 函数的源代码发现其没使用进行缓存,但是 wp_nav_menu 又没有足够多的 hook,于是我只能重写了一个 wpjam_nav_menu 函数,使用 WordPress Transients API 对生成的菜单进行缓存,大大提高页面的效率:

/*
Plugin Name: WPJAM Memcached Nav Menu
Plugin URI: http://blog.wpjam.com/m/wpjam_nav_menu/
Description: 使用内存缓存提高 wp_nav_menu 效率
Version: 0.1
Author: Denis
Author URI: http://blog.wpjam.com/
*/
//给 wp_nav_menu 加上对象缓存,加快效率
function wpjam_nav_menu( $args = array() ) {
    static $menu_id_slugs = array();

    $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '',
    'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '

<ul id="%1$s" class="%2$s">%3$s</ul>
',
    'depth' => 0, 'walker' => '', 'theme_location' => '' );

    $args = wp_parse_args( $args, $defaults );
    $args = apply_filters( 'wp_nav_menu_args', $args );
    $args = (object) $args;

    // Get the nav menu based on the requested menu
    $menu = wp_get_nav_menu_object( $args->menu );

    // Get the nav menu based on the theme_location
    if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
        $menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );

    // get the first menu that has items if we still can't find a menu
    if ( ! $menu && !$args->theme_location ) {
        $menus = wp_get_nav_menus();
        foreach ( $menus as $menu_maybe ) {
            if ( $menu_items = wpjam_get_nav_menu_items( $menu_maybe->term_id, array( 'update_post_term_cache' => false ) ) ) {
                $menu = $menu_maybe;
                break;
            }
        }
    }

    // If the menu exists, get its items.
    if ( $menu && ! is_wp_error($menu) && !isset($menu_items) )
        $menu_items = wpjam_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );

    /*
     * If no menu was found:
     *  - Fallback (if one was specified), or bail.
     *
     * If no menu items were found:
     *  - Fallback, but only if no theme location was specified.
     *  - Otherwise, bail.
     */
    if ( ( !$menu || is_wp_error($menu) || ( isset($menu_items) && empty($menu_items) && !$args->theme_location ) )
        && $args->fallback_cb && is_callable( $args->fallback_cb ) )
            return call_user_func( $args->fallback_cb, (array) $args );

    if ( !$menu || is_wp_error( $menu ) || empty( $menu_items ) )
        return false;

    $nav_menu = $items = '';

    $show_container = false;
    if ( $args->container ) {
        $allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
        if ( in_array( $args->container, $allowed_tags ) ) {
            $show_container = true;
            $class = $args->container_class ? ' class="' . esc_attr( $args->container_class ) . '"' : ' class="menu-'. $menu->slug .'-container"';
            $id = $args->container_id ? ' id="' . esc_attr( $args->container_id ) . '"' : '';
            $nav_menu .= '<'. $args->container . $id . $class . '>';
        }
    }

    // Set up the $menu_item variables
    _wp_menu_item_classes_by_context( $menu_items );

    $sorted_menu_items = array();
    foreach ( (array) $menu_items as $key => $menu_item )
        $sorted_menu_items[$menu_item->menu_order] = $menu_item;

    unset($menu_items);

    $sorted_menu_items = apply_filters( 'wp_nav_menu_objects', $sorted_menu_items, $args );

    $items .= walk_nav_menu_tree( $sorted_menu_items, $args->depth, $args );
    unset($sorted_menu_items);

    // Attributes
    if ( ! empty( $args->menu_id ) ) {
        $wrap_id = $args->menu_id;
    } else {
        $wrap_id = 'menu-' . $menu->slug;
        while ( in_array( $wrap_id, $menu_id_slugs ) ) {
            if ( preg_match( '#-(\d+)$#', $wrap_id, $matches ) )
                $wrap_id = preg_replace('#-(\d+)$#', '-' . ++$matches[1], $wrap_id );
            else
                $wrap_id = $wrap_id . '-1';
        }
    }
    $menu_id_slugs[] = $wrap_id;

    $wrap_class = $args->menu_class ? $args->menu_class : '';

    // Allow plugins to hook into the menu to add their own 

<li>'s
    $items = apply_filters( 'wp_nav_menu_items', $items, $args );
    $items = apply_filters( "wp_nav_menu_{$menu->slug}_items", $items, $args );

    $nav_menu .= sprintf( $args->items_wrap, esc_attr( $wrap_id ), esc_attr( $wrap_class ), $items );
    unset( $items );

    if ( $show_container )
        $nav_menu .= '</' . $args->container . '>';

    $nav_menu = apply_filters( 'wp_nav_menu', $nav_menu, $args );

    if ( $args->echo )
        echo $nav_menu;
    else
        return $nav_menu;
}
function wpjam_get_nav_menu_items( $menu, $args = array() ) {
    $menu = wp_get_nav_menu_object( $menu );

    $menu_items = get_transient('wpjam_nav_menu_'.$menu->term_id);
    if($menu_items === false){
        $menu_items = wp_get_nav_menu_items( $menu->term_id, $args );
        set_transient('wpjam_nav_menu_'.$menu->term_id, $menu_items, 3600);
    }

    return $menu_items;
}

//后台更新自定义菜单的时候,更新缓存
add_action( 'wp_update_nav_menu', 'wpjam_update_nav_menu' );
function wpjam_update_nav_menu( $menu_id = null, $menu_data = null ) {
    delete_transient('wpjam_nav_menu_'.$menu_id);
}

将上面的函数添加到当前主题的 functions.php 文件,或者直接上传为一个插件并激活。

然后在主题模板中(一般在 header.php 文件中)使用 wpjam_nav_menu 函数替代原来的 wp_nav_menu 即可

标签:wordpress教学

下一篇:【wordpress教学】WordPress 发布 3.7.1 维护更新版本

上一篇:【wordpress教学】WordPress 技巧:用户注册时候不能含有非法关键字

相关文章

  575

注册时间:

网站:1 个   小程序:3 个  文章:12 篇

  • 315

    网站

  • 12

    小程序

  • 35791

    文章

  • 69

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定