0

I am listing my posts alphabetically, on top of each section I am displaying the initial letter and the php code is this

<?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();
    ?>

with and HTML output of

<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>

Now my problem is that I need to insert a div with a data-role of collapsible so it can look like

<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>

but I can't. I have tried to echo out the before the h3 and after the if but it doesn't get me the markup I need. Any help?

2 Answers 2

2

Let's try this code:

<?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();
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes that's the one! I just changed <div class="entry-content"> for data-role="collapsable" and works perfectly! Thanks a lot! still I am trying to understand how you did it
It's not so complicated. just add a flag to determine when you open the "div" tag for the first time. then every time you open a div you have to close it. So I checked if it has already opened by checking the flag. Since the last closing tag is skipped, you need to add another checking after endwhile. That's all.
I know.. I tried, it says I can't vote because I need reputation and I just signed in :(
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();
?>

1 Comment

Yes I tried that too and it prints me back '<div data-role="collapsible"> <h3>A</h3> <p></p> <p></p> <p></p> <p></p> <p></p> <div data-role="collapsible"> <h3>B</h3> <p></p> <p></p> <p></p> <p></p> <p></p> <div data-role="collapsible"> <h3>C</h3> <p></p> <p></p> <p></p> <p></p> <p></p>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.