在A-Z列表代码的顶部添加A-Z菜单

时间:2013-05-07 作者:Mark Cursley

我有以下代码,可以在自定义帖子类型中创建我的帖子的a和a-Z列表。我想以某种方式在顶部创建一个a-Z菜单,该菜单向下链接到其各自的锚点。我似乎无法让这部分发挥作用。非常感谢您的帮助?

    <?php
   $args = array(
    \'post_type\' => bha_issues, 
    //\'post_status\' => \'bha_issues\',
     \'orderby\' => \'title\',
     \'order\' => \'ASC\',
     \'caller_get_posts\' => 1,
     \'posts_per_page\' => 40,
                 );
query_posts($args);





if (have_posts()) {
    $curr_letter = \'\';
    while (have_posts()) {
        the_post();
        $this_letter = strtoupper(substr($post->post_title,0,1));

        if ($this_letter != $curr_letter) {
          echo "<div id=\'$this_letter\' class=\'hidden\'><a name=\'$this_letter\'></a><h2>$this_letter </h2></div>";
          $curr_letter = $this_letter;
        }


    ?>

        <div id=\'$this_letter\' class=\'hidden\'><a href=\'<?php the_permalink() ?>\'><?php the_title(); ?></a></div>
    <?php 
    }
}
?>

1 个回复
SO网友:vancoder

我以前也做过类似的工作,用于词汇表页面。我的代码如下。希望你能由此推断。注意,I’;m正在使用WP_Queryquery_posts, 因为这是最佳实践。

<?php
    $args = array(
        \'post_type\' => \'my_glossary_term\',
        \'orderby\' => \'title\',
        \'order\' => \'ASC\',
        \'posts_per_page\' => -1
    );

    $query = new WP_Query( $args );

    $dl = \'\';
    $glossary_letter = \'\';
    $active_letters = array( );

    while ( $query->have_posts() ) {
        $query->next_post();
        $term_letter = strtoupper( substr( $query->post->post_title, 0, 1 ) );
        if ( $glossary_letter != $term_letter ) {
            $dl .= (count( $active_letters ) ? \'</dl>\' : \'\') . \'<li id="\' . $term_letter . \'"><span class="subheading">\' . $term_letter . \'</span><dl>\';
            $glossary_letter = $term_letter;
            $active_letters[] = $term_letter;
        }
        $dl .= \'<dt>\' . $query->post->post_title . \'</dt>\';
        $dl .= \'<dd>\' . $query->post->post_content . \'</dd>\';
    }
    $dl .= \'</dl></li>\';

    $ul = \'<ul class="letters">\';
    foreach ( array( \'A\', \'B\', \'C\', \'D\', \'E\', \'F\', \'G\', \'H\', \'I\', \'J\', \'K\', \'L\', \'M\', \'N\', \'O\', \'P\', \'Q\', \'R\', \'S\', \'T\', \'U\', \'V\', \'W\', \'X\', \'Y\', \'Z\' ) as $letter ) {
        $ul .= \'<li>\' . (in_array( $letter, $active_letters ) ? \'<a href="#\' . $letter . \'">\' . $letter . \'</a>\' : $letter) . \'</li>\';
    }
    $ul .= \'</ul>\';

    echo \'<div id="glossary">\' . $ul . \'<ul class="definitions">\' . $dl . \'</ul></div>\';
    ?>

结束

相关推荐