0

我正在使用 wordpress 开发一个网站。我已经包含了一个选择类型菜单,其中包含一个父页面的子页面的名称和链接。一切正常,我的页面按字母顺序 ASC 排序:

Bali
Bruxelles
Chicago
Edinburgh
Geneve
Los Angeles
Lyon
Miami
Paris
San Diego
San Francisco
Vancouver
Washington
Zurich

这是我的代码:

<div class="styled-select">
<?php

if(!$post->post_parent){

    $children = get_pages(array(
        'child_of' => $post->ID,
        'post_type' => 'page',
        'post_status' => 'publish',
        'sort_order' => 'ASC',
        'sort_column' => 'post_title',
    ));

}else{

    $children = get_pages(array(
        'child_of' => $post->post_parent,
        'post_type' => 'page',
        'post_status' => 'publish',
        'sort_order' => 'ASC',
        'sort_column' => 'post_title',
    ));
}

if ($children) {
    echo '<select name="" onchange="location = this.options[this.selectedIndex].value;">';
    echo '<option>'. 'A - Z' .'</option>';

    foreach($children as $child){
        //print_r($child);
        $permalink = get_permalink($child->ID);

        echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
    }
    echo '</select>';

} ?>

</div>

我想知道是否可以像这样在我的菜单中自动添加字母索引(猜测中的宽度)。在所有以 A 开头的页面之前添加“A”(当有以 A 开头的页面时......),在所有以 B 开头的页面之前添加“B”,依此类推......必须看起来像这样的东西:

A
B
  Bali
  Bruxelles
C
  Chicago
D
E
F 
  Edinburgh
G
  Geneve
H
I
J
K
L
  Los Angeles
  Lyon
M
  Miami
N
O
P
  Paris
Q
R
S
  San Diego
  San Francisco
T
U
V
  Vancouver
W
  Washington
X
Y
Z
  Zurich

如果有人知道我能做到这一点,将不胜感激!多谢

马蒂厄

4

2 回答 2

1

使用如下(仅用于演示):

    function getInitials($name){
//split name using spaces
$words=explode(" ",$name);
$inits='';
//loop through array extracting initial letters
    foreach($words as $word){
    $inits.=strtoupper(substr($word,0,1));
    }
return $inits;  
}
$currval = "";
foreach($children as $child){
    //print_r($child);
    $permalink = get_permalink($child->ID);
    $initial = getInitials($child->post_title);
    if($initial!='' && $currval != $initial) {
        $currval = $initial;
        echo '<optgroup>'.$initial.'</option>';
    }
    echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
}

虽然没有检查..如果您遇到任何问题,请检查并返回


如果您需要显示所有字母,我可以给您一个想法

function getInitials($name){
//split name using spaces
$words=explode(" ",$name);
$inits='';
//loop through array extracting initial letters
    foreach($words as $word){
    $inits.=strtoupper(substr($word,0,1));
    }
return $inits; 
}
$currval = "";


$alp = array('A'...'Z')

foreach($alp as $abc) {
    foreach($children as $child){
        //print_r($child);
        $permalink = get_permalink($child->ID);
        $initial = getInitials($child->post_title);
        if($initial!='' && $currval != $initial && $initial == $abc) {
            $currval = $initial;
            echo '<optgroup>'.$initial.'</option>';
        }
        echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
    }
}
于 2013-10-02T08:27:50.127 回答
0

您好,我已根据您的需要再次编辑了代码。

请检查

<?php
function getInitials($name){
    //split name using spaces
    $words=explode(" ",$name);
    $inits='';
    //loop through array extracting initial letters
    foreach($words as $word){
        $inits = strtoupper(substr($word,0,1));
        break;
    }
    return $inits; 
}
$currval = "";
$alp = array('A','B','C','D','E','Y','S','Z');
//$children = array('Asin','Bali','Bruxelles','Chicago','Denmark','Edinburgh','Flag','San Francisco');
?>
<select name="test">
<?php
foreach($alp as $key => $abc) {
    $showinOpt = false;
    foreach($children as $child) {
      $permalink = get_permalink($child->ID);
      $initial = getInitials($child->post_title);
       if($initial == $abc) {
           if($currval != $initial) {
                $currval = $initial;
                echo '<optgroup label="'.$initial.'">'.$initial.'</optgroup>';
                $showinOpt = true;
           }
           echo '<option value="'.$permalink.'">'.$child->post_title.'</option>';
        }
    }
    if(!$showinOpt) {
        echo '<optgroup label="'.$abc.'">'.$abc.'</optgroup>';
    }
}
?>
</select>

在你得到 $children 数组后添加它。

于 2013-10-02T11:32:03.590 回答