2016-07-26 11 views
1

WordPressのページで、そのページのすべての子を表示したいとします。これは、このように、現時点では動作します:WordPress:子ページ数をカウントする

<?php 
$args = array( 
'post_type'  => 'page', 
'posts_per_page' => -1, 
'post_parent' => $post->ID, 
'order'   => 'ASC', 
'orderby'  => 'menu_order' 
); 
$parent = new WP_Query($args);    
if ($parent->have_posts()) : ?>    
<?php while ($parent->have_posts()) : $parent->the_post(); ?>    
    <div id="parent-<?php the_ID(); ?>" class="parent-page">    
     <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
    </div> 
<?php endwhile; ?> 

私が欲しいのはループが私の記事をカウントし、例えば、1から始まる、DIVに番号を印刷していることである:

<div class="child1"> 
    Title of first child 
</div> 
<div class="child2"> 
    Title of second child 
</div> 
<div class="child3"> 
    Title of third child 
</div> 

あなたは何ですか提案?

+1

あなたの助けが必要なのは明確ではありません。ループ内のマークアップを変更するだけでいいようですか? – George

答えて

2

$count変数を作成して、ループのたびにその変数を増やしてください。

<?php $count = 1; ?> 
<?php while ($parent->have_posts()) : $parent->the_post(); ?>    
    <div id="parent-<?php the_ID(); ?>" class="parent-page child<?php echo $count++; ?>">    
     <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
    </div> 
<?php endwhile; ?> 
+0

偉大な、それは:) – Irene

関連する問題