WordPress英文标签按A-Z分类排序的页面

这个功能是给wordpress建立一个标签(tags)页面,根据字母 A-Z 排序,然后将非字母(比如数字或标点符号)的另外排序,不过可惜的是只支持英文。

实现方法就是用 PHP 判断英文tag的第一个字母,然后做归类排列,代码如下:

<?php
/*
    Template Name: Tag Index
*/
/* Begin Tag Index */ 
 
// Make an array from A to Z.
$characters = range('A','Z');
 
// Retrieve all tags
$getTags = get_tags( array( 'order' => 'ASC') );
 
// Retrieve first letter from tag name
$isFirstCharLetter = ctype_alpha(substr($getTags[0]->name, 0, 1));
 
 
                                                
// Special Character and Number Loop
// Run a check to see if the first tag starts with a letter
// If it does not, run this
if ( $isFirstCharLetter == false ){
 
        // Print a number container        
        $html .= "<div class='tag-group'>";                                        
        $html .= "<h3 class='tag-title'>#</h3>";
        $html .= "<ul class='tag-list'>";
        
        // Special Character/Number Loop
        while( $isFirstCharLetter == false ){
        
                // Get the current tag
                $tag = array_shift($getTags);
                
                // Get the current tag link 
                $tag_link = get_tag_link($tag->term_id);
                
                // Print List Item
                $html .= "<li class='tag-item'>";
                
                // Check to see how many tags exist for the current letter then print appropriate code
        if ( $tag->count > 1 ) {
            $html .= "<p><a href='{$tag_link}' title='View all {$tag->count} articles with the tag of {$tag->name}' class='{$tag->slug}'>";
        } else {
            $html .= "<p><a href='{$tag_link}' title='View the article tagged {$tag->name}' class='{$tag->slug}'>";
        }
        
        // Print tag name and count then close the list item
                $html .= "<span class='tag-name'>{$tag->name}</span></a><span class='tag-count'>#{$tag->count}</span></p>";                                                                
                $html .= "</li>";
                
                // Retrieve first letter from tag name
                // Need to redefine the global variable since we are shifting the array
                $isFirstCharLetter = ctype_alpha(substr($getTags[0]->name, 0, 1));
                
        }
        
        // Close the containers
        $html .= "</ul>";
        $html .= "</div>";        
}
 
// Letter Loop
do {
        
        // Get the right letter
        $currentLetter = array_shift($characters);
 
        // Print stuff        
        $html .= "<div class='tag-group'>";                                        
        $html .= "<h3 class='tag-title'>{$currentLetter}</h3>";
        $html .= "<ul class='tag-list'>";
        
        // While we have tags, run this loop
        while($getTags){
        
                // Retrieve first letter from tag name
                $firstChar = substr($getTags[0]->name, 0, 1);
        
                // Does the first letter match the current letter?
                // Check both upper and lowercase characters for true
                if ( strcasecmp($currentLetter, $firstChar) == 0 ){        
                                                                        
                        // Get the current tag
                        $tag = array_shift($getTags);
                        
                        // Get the current tag link 
                        $tag_link = get_tag_link($tag->term_id);
                        
                        // Print stuff
                        $html .= "<li class='tag-item'>";
                        
                        // Check to see how many tags exist for the current letter then print appropriate code
            if ( $tag->count > 1 ) {
                $html .= "<p><a href='{$tag_link}' title='View all {$tag->count} articles with the tag of {$tag->name}' class='{$tag->slug}'>";
            } else {
                $html .= "<p><a href='{$tag_link}' title='View the article tagged {$tag->name}' class='{$tag->slug}'>";
            }
            
            // Print more stuff
                        $html .= "<span class='tag-name'>{$tag->name}</span></a><span class='tag-count'>#{$tag->count}</span></p>";                                                                
                        $html .= "</li>";
                        
                } else {
                        break 1;
                }
        }                                                                
 
        $html .= "</ul>";
        $html .= "</div>";
} while ( $characters ); // Will loop over each character in the array
 
// Let's see what we got:
echo($html);
 
?>

不知道中文的怎么搞,如果按照上面的思路,那就是要将中文标签的首字翻译成拼音,然后按照拼音首字母来排列,这么搞不是要新建个表格存储tag的拼音么?搞不定,有高手帮忙解决么?

以上英文的方法转自老外博客:stacigh.com/2013/11/creating-a-wordpress-tag-index-page/

10 Replies to “WordPress英文标签按A-Z分类排序的页面”

发表评论

电子邮件地址不会被公开。 必填项已用*标注