如何在这个PHP代码中插入div

时间:2013-08-22 17:14:52

标签: php html

我按字母顺序列出我的帖子,在每个部分的顶部我显示的是首字母,而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之后,但它没有得到我需要的标记。有帮助吗?

2 个答案:

答案 0 :(得分: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();
?>

答案 1 :(得分: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();
?>