按字母对分类术语进行分组和排序-有更好的方法吗?

时间:2016-09-29 作者:Mike

背景:我在一个游戏博客上工作,我为游戏创建了一个自定义分类法。

我添加了一个游戏列表页面,它将(毫不奇怪地)列出所有non-empty 该分类法中的术语。知道这个列表将来可能会变得相当大,我想通过页面顶部的快速访问栏和每个部分末尾的“返回顶部”链接,按照术语的第一个字符对其进行排序和分组。

我想出了以下函数来检索排序列表,它工作得很好,但我想知道是否有更有效的方法来实现这一点(比如我还没有发现的一些核心选项)。

function bmg_getGameList() {
    $games = get_terms(BMG_GAME_SLUG, true);

    if ($games && !is_wp_error($games)) {
        $gameList = array(
            "#" =>  array(),    "A" =>  array(),    "B" =>  array(),
            "C" =>  array(),    "D" =>  array(),    "E" =>  array(),
            "F" =>  array(),    "G" =>  array(),    "H" =>  array(),
            "I" =>  array(),    "J" =>  array(),    "K" =>  array(),
            "L" =>  array(),    "M" =>  array(),    "N" =>  array(),
            "O" =>  array(),    "P" =>  array(),    "Q" =>  array(),
            "R" =>  array(),    "S" =>  array(),    "T" =>  array(),
            "U" =>  array(),    "V" =>  array(),    "W" =>  array(),
            "X" =>  array(),    "Y" =>  array(),    "Z" =>  array(),
        );

        foreach($games as $game) {
            $normalizedTitle = strtoupper($game->name);
            $normalizedTitle = preg_replace("/^THE ",\'\', $normalizedTitle,1);

            /* NOTE:    strncmp returns 0 if the comparison is EQUAL. 
                        We want equality in this case since we\'re 
                        sorting games into buckets based on first 
                        characters */
            if (strncmp($normalizedTitle, "A", 1) === 0) {
                $gameList["A"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "B", 1) === 0) {
                $gameList["B"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "C", 1) === 0) {
                $gameList["C"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "D", 1) === 0) {
                $gameList["D"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "E", 1) === 0) {
                $gameList["E"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "F", 1) === 0) {
                $gameList["F"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "G", 1) === 0) {
                $gameList["G"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "H", 1) === 0) {
                $gameList["H"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "I", 1) === 0) {
                $gameList["I"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "J", 1) === 0) {
                $gameList["J"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "K", 1) === 0) {
                $gameList["K"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "L", 1) === 0) {
                $gameList["L"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "M", 1) === 0) {
                $gameList["M"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "N", 1) === 0) {
                $gameList["N"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "O", 1) === 0) {
                $gameList["O"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "P", 1) === 0) {
                $gameList["P"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "Q", 1) === 0) {
                $gameList["Q"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "R", 1) === 0) {
                $gameList["R"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "S", 1) === 0) {
                $gameList["S"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "T", 1) === 0) {
                $gameList["T"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "U", 1) === 0) {
                $gameList["U"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "V", 1) === 0) {
                $gameList["V"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "W", 1) === 0) {
                $gameList["W"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "X", 1) === 0) {
                $gameList["X"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "Y", 1) === 0) {
                $gameList["Y"][$normalizedTitle] = $game;
            } else if (strncmp($normalizedTitle, "Z", 1) === 0) {
                $gameList["Z"][$normalizedTitle] = $game;
            } else {
                $gameList["#"][$normalizedTitle] = $game;
            }
        }

        /*  Sort each letter bucket by the NORMALIZED titles
            (case insensitive, ignoring "THE" - The Division 
            sorts under D) */
        foreach ($gameList as $group) {
            ksort($group);
        }

        return $gameList;
    } else {
        return false;
    }
}

1 个回复
SO网友:Benoti

我是这样做的,

add_action(\'the_content\', \'lm_alpha_order\');

function lm_alpha_order($content){


if(is_page(\'alphabetique\')){

    $args = array(
        \'taxonomy\' => \'media_tag\',
        \'hide_empty\' => true,
        \'orderby\' => \'name\',
        \'order\'   => \'ASC\'
    );

    $terms = get_terms(\'media_tag\', $args);

    $azRange = range(\'A\', \'Z\');

    echo \'<div class="half left" style="float:left;padding-left: 2em;">\';

    foreach ($azRange as $letter)
    {
        $letter_outputs = \'\';
        $the_letter =false;
        //print("$letter\\n");
        $count = 0;
        $ul = \'<ul class="alpha">\';
        foreach ( $terms as $term ) {
            if ( preg_match("/^$letter/i", $term->name) ) {
                $the_letter = true;

                $args_post = array(
                    \'posts_per_page\'   => -1,
                    \'tax_query\' => array(
                        \'relation\'=> \'AND\',
                        array(
                            \'taxonomy\' => \'media_tag\',
                            \'field\'    => \'term_id\',
                            \'terms\'    => $term->term_id,
                        ),
                        array(
                            \'taxonomy\' => \'media_category\',
                            \'field\'    => \'slug\',
                            \'terms\'    => \'audio\',

                        ),
                    ),
                    \'post_type\'        => \'post\',
                    \'post_status\'      => \'publish\',
                    \'suppress_filters\' => true
                );

                $posts_array = new WP_Query( $args_post );
                $inner_letter_outputs =\'\';
                $letter_outputs .= $ul;
                $ul = \'\';  // only display once
                $letter_outputs .= \'<li><h3><a href="javascript:void(0);" data-toggle="collapse" data-target="#media_\'.$term->slug.\'">\'. $term->name . \'</a></h3>
                <div id="media_\'.$term->slug.\'" class="collapse" style="color:#000035;">\';
                    foreach($posts_array->posts as $post) {
                        if ($post->ID != null) {
                            if(has_shortcode($post->post_content, \'audio\')){
                               $media_url = get_mp3_url($post->post_content);
                                foreach($media_url[0] as $k=>$url){
                                    if($k == \'mp3\'){
                                        $url = str_replace(\'"\', \'\',$url);
                                        $media_link = $url;
                                    }
                                }
                            }

                            $inner_letter_outputs .= \'
                        <span class="media_\'.$post->ID.\' media-title"><a href="\'.get_permalink($post->ID).\'">\' . $post->post_title . \'</a></span><br/>
                        <span class="media-button"><a href="javascript:void(0):" class="btn-play-sound" data-item="\'.$post->ID.\'" data-guid="\'.$media_link.\'" data-title="\'.$post->post_title.\'">
                        <i class="fa fa-play" aria-hidden="true"></i></a>  <a href="javascript:void(0):" class="btn-play-pause" data-item="\'.$post->ID.\'" data-guid="\'.$media_link.\'"><i class="fa fa-pause" aria-hidden="true"></i></a>  
                        <a href="\'.home_url().\'/download.php?file=\'.$media_link.\'" target="_blank" title="\'.__(\'Download files\', \'laura-marine\').\'"><i class="fa fa-download" aria-hidden="true"></i></a>
                        </span>
                        <br/>\';
                        }
                    }
                $letter_outputs .= $inner_letter_outputs;
                $letter_outputs .=\'</div></li>\';

                $count += sizeof($posts_array->posts);
            }
        }
        if ( $ul == \'\') {
            $letter_outputs .= \'</ul>\';
        }
        if($the_letter == true){
            echo \'<h2>\'.$letter . \' <small>(\'.$count.\')</small></h2>\';
        }
        echo $letter_outputs;
    }

    echo \'</div>\';
    $audio_shortcode = do_shortcode(\'[audio mp3="http://www.example.com/wp-content/uploads/2016/05/sound.mp3"][/audio]\');
    echo \'<div class="half right" style="position:relative; ">
            <div class="lm-sound-player" style="">
                <div class="hidden-player" style="display:none;">
                \'.$audio_shortcode.\'
                </div>
                <div class="player-tweet">
                    <i class="fa fa-spinner fa-pulse fa-4x fa-fw"></i><br/>
                        <span class="sr-only sound-title">Choose sound to listen...</span>
                </div>
            </div>
          </div>\';

}

return $content;
}