创建列,垂直和按字母顺序排序,如何?

时间:2014-08-24 09:30:30

标签: php css wordpress

更新:更好的代码,谢谢!

这就是我想要的:

A   D
-   -
-   -
-   -
-   -   
B   E
-   -
-   -
-   -
-   -
C   F
-   -
-   -
-   -
-   -

尝试创建9列,输出垂直和按字母顺序排序的帖子列表。有了一点帮助,我有一个循环,我认为输出正确,但不确定,我不知道如何设计它。我得到一个直列,或水平排序。这是循环和测试方:http://skateflix.se/test2/

 <?php

   $last_char = '';
   $args=array(
   'post_type' => 'portfolio',
   'orderby' => 'title',
   'order' => 'ASC',
   'posts_per_page'=>-1,
   'portfolio-category' => 'indie',
   'ignore_sticky_posts'=>1
 );

 $my_query = new WP_Query($args);

 $columnCount = 0;

 ?>

<?php if( $my_query->have_posts() ) : ?>
 <?php  echo 'Alphabetic index of all ' . count($my_query->posts) . ' posts'; ?>

<table>
    <tr>
        <?php while ($my_query->have_posts()) : ?>
            <?php if ($columnCount == 8): ?>
                </tr>
                <tr>
                <?php $columnCount = 0; ?>
            <?php endif; ?>

            <td>
            <?php $my_query->the_post(); ?>
            <?php $this_char = strtoupper(substr($post->post_title,0,1));  

             if ($this_char != $last_char) : ?>
          </table></td><td>
       <?php   $last_char = $this_char; ?>
       <h2> <?= $last_char; ?></h2>
      <table>
     <?php else: ?>
      <tr><td><p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent     Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p></td></tr>

 <?php endif; ?>


        <?php endwhile; ?>  
        <?php if ($columnCount != 8): ?>
            </tr><!-- Make sure the last row gets closed. -->
        <?php endif; ?>
   </table>
  <?php endif; ?>

1 个答案:

答案 0 :(得分:0)

在黑暗中拍摄,但试试这个:

 <?php if ($this_char != $last_char) :
     </table></td><td>
     $last_char = $this_char; ?>
     <h2> <?= $last_char; ?></h2>
     <table>
 <?php else: ?>
         <tr><td><p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p></td></tr>

 <?php endif; ?>

确保在</td>

之前摆脱<?php endwhile; ?>