0

我按字母顺序列出我的帖子,在每个部分的顶部我显示首字母,php 代码是这样的

<?php
        $args=array(
          'post_type' => 'books',
          'orderby' => 'title',
          'order' => 'ASC',
          'posts_per_page'=>-1,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post();?>
            <?php
            $this_char = strtoupper(substr($post->post_title,0,1));
            if ($this_char != $last_char) {
              $last_char = $this_char;
              echo '<h3>'.$last_char.'</h3>';

            } ?>
            <p><a data-transition="slide" href="<?php the_permalink() ?>"     rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php endwhile; } 
        wp_reset_query();
    ?>

和 HTML 输出

<div class="entry-content">
<h3>A</h3>
<p></p>
<p></p>
<p></p>
<p></p>
<h3>B</h3>
<p></p>
<p></p>
<p></p>
</div>

现在我的问题是我需要插入一个具有可折叠数据角色的 div,所以它看起来像

<div class="entry-content">
<div dat-role="collapsible">
    <h3>A</h3>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
</div>
<div dat-role="collapsible">
    <h3>B</h3>
    <p></p>
    <p></p>
    <p></p>
    <p></p>
</div>
</div>

但我不能。我试图在 h3 之前和 if 之后回显,但它没有得到我需要的标记。有什么帮助吗?

4

2 回答 2

2

让我们试试这段代码:

<?php
    $args=array(
      'post_type' => 'books',
      'orderby' => 'title',
      'order' => 'ASC',
      'posts_per_page'=>-1,
      'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();?>
        <?php
        $this_char = strtoupper(substr($post->post_title,0,1));
        if ($this_char != $last_char) {
          $last_char = $this_char;
          if (isset($flag)) 
            echo '</div>';              
          echo '<div class="entry-content">';
          echo '<h3>'.$last_char.'</h3>';
          $flag = true;
        } ?>
        <p><a data-transition="slide" href="<?php the_permalink() ?>"     rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php endwhile; } 
        if (isset($flag)) 
            echo '</div>';  
    wp_reset_query();
?>
于 2013-08-22T17:26:36.863 回答
1
<?php
        $args=array(
          'post_type' => 'books',
          'orderby' => 'title',
          'order' => 'ASC',
          'posts_per_page'=>-1,
          'caller_get_posts'=>1
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post();?>
            <?php
            $this_char = strtoupper(substr($post->post_title,0,1));
            if ($this_char != $last_char) {
              $last_char = $this_char;
              echo '<div dat-role="collapsible"><h3>'.$last_char.'</h3>';

            } ?>
            <p><a data-transition="slide" href="<?php the_permalink() ?>"     rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php endwhile; } 
            echo "</div>";
        wp_reset_query();
?>
于 2013-08-22T17:18:20.613 回答