drkrsx3135168 2013-11-03 22:29
浏览 24
已采纳

按名称对帖子进行排序,并在wordpress中仅显示一次首字母

I've got a problem, I display posts in wordpress ordered by title in ascending order, but what I want is to display first letter of group of posts, I mean

A: Annanas
   Apple
   Almond
B: Banana
G: Grape

I don't know how to implement something like this in standard loop

<?php query_posts( array ( 'category_name' => 'publishers', 'orderby' => 'title', 'order' => 'ASC' ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <li>
        <?php the_title(); ?>
    </li>
<?php endwhile; endif; wp_reset_query(); ?>

I'd be very happy if somebody can help me…

Take care and have a nice day!

  • 写回答

1条回答 默认 最新

  • dongxixian7803 2013-11-03 22:47
    关注

    An idea would be:

    <?php 
    $letter=' '; 
    query_posts( array ( 'category_name' => 'publishers', 'orderby' => 'title', 'order' => 'ASC' ) );
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <li>
            <?php 
            $title=get_the_title(); 
            $initial=strtoupper(substr($title,0,1));
            if($initial!=$letter)
              {
              echo "<span>$initial : </span>";
              $letter=$initial;
              }
            echo $title;
            ?>
        </li>
    <?php endwhile; endif; wp_reset_query(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划