Php WordPress:计数子页面
在WordPress的一个页面上,我想显示该页面的所有子页面。目前的工作原理如下:Php WordPress:计数子页面,php,wordpress,count,Php,Wordpress,Count,在WordPress的一个页面上,我想显示该页面的所有子页面。目前的工作原理如下: <?php $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $parent = ne
<?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; ?>
只需创建一个$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; ?>
你需要什么帮助还不清楚。似乎您只需要修改循环中的标记?太好了,就是这样:)
<?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; ?>