Reputation: 66
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?
Upvotes: 0
Views: 217
Reputation: 1001
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();
?>
Upvotes: 2
Reputation: 207900
<?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();
?>
Upvotes: 1