按首字母显示术语

时间:2011-12-18 作者:Catiebug

我开了一个书评博客,我有多个页面,按不同的内容(评级、书名和作者)列出了书评

我有针对作者的自定义分类法,我的代码基本上列出了所有包含帖子的术语,然后列出帖子。从第一个字母来看,他们都是按字母顺序排列的。我想做的是让它抓住第一个字母,然后用这种方式把所有的东西都切掉。现在看起来:

Bray, Libba

<选美皇后

Elkeles, Simone

<链式反应

B

布雷,利巴

选美皇后

E

Elkeles,Simone

链式反应,所以基本上只显示我现有的术语,但每个字母都有一个部分,然后显示从该字母开始的术语,然后列出帖子。

<?php
$taxonomy = \'authors\';//  e.g. post_tag, category
$param_type = \'authors\'; //  e.g. tag__in, category__in
$term_args=array(
  \'orderby\' => \'name\',
  \'order\' => \'ASC\'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
  foreach( $terms as $term ) {
    $args=array(
      "$param_type" => $term->slug,
      \'post_type\' => \'reviews\',
      \'post_status\' => \'publish\',
      \'posts_per_page\' => -1,
      \'caller_get_posts\'=> 1,
      \'meta_key\' => \'booktitle\', 
      \'orderby\' => \'meta_value\',
      \'order\' => \'ASC\'
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {

      echo \'<h2>\'.$term->name.\'</h2>\';
      while ($my_query->have_posts()) : $my_query->the_post(); 
  ?>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo c2c_get_custom(\'booktitle\'); ?></a> by <?php echo c2c_get_custom(\'author\'); ?><br />
       <?php
      endwhile;
    }
  }
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

1 个回复
最合适的回答,由SO网友:Joshua Abenazer 整理而成

Try this code.

<?php
$taxonomy = \'authors\';//  e.g. post_tag, category
$param_type = \'authors\'; //  e.g. tag__in, category__in
$term_args=array(
            \'orderby\' => \'name\',
            \'order\' => \'ASC\'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
    $first_letter = null;
    foreach( $terms as $term ) {

        $flag = 0;

        if( $first_letter != substr( $term->name, 0, 1 ) ) {
          $first_letter = substr( $term->name, 0, 1 );
          $flag = 1;
        }

        if( $flag ) {
          echo \'<strong>\'.$first_letter.\'</strong>\'; // Output the first letter
        }

        $args=array(
          "$param_type" => $term->slug,
          \'post_type\' => \'reviews\',
          \'post_status\' => \'publish\',
          \'posts_per_page\' => -1,
          \'caller_get_posts\'=> 1,
          \'meta_key\' => \'booktitle\', 
          \'orderby\' => \'meta_value\',
          \'order\' => \'ASC\'
          );

        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
            echo \'<h2>\'.$term->name.\'</h2>\';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php echo c2c_get_custom(\'booktitle\'); ?></a> by <?php echo c2c_get_custom(\'author\'); ?><br /><?php
            endwhile;
        }
    }
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
结束